-1

次のエラーが発生します。

/calcTax.rb:9: syntax error, unexpected $undefined
  grandtotal = $#{subtotal - tax}

このコードから:

print('What amount would you like to calculate tax for?  $')
subtotal = gets.to_i
taxrate = 0.078
tax = subtotal * taxrate
if (tax > 0.0)
  grandtotal = $#{subtotal + tax}
else if (tax < 0.0)
  grandtotal = $#{subtotal - tax}
puts "Tax on $#{subtotal} is $#{tax}, so the grandtotal is $#{grandtotal}."

subtotal別の方法で値を設定する必要があるのか​​、それともプログラムを修復するために何ができるのか疑問に思っています。

10行目でもunexpected $endエラーが発生しています。

4

1 に答える 1

1

ここでの構文にはいくつか問題があります。

まず、$#{blah}構文は、引用符で囲まれた文字列に変数を挿入するときにのみ必要です(そして有効なだけです!)。計算を実行しているときは、次のように簡単に言うことができます。

grandtotal = subtotal + tax

thenまた、両方のif行の最後にaを追加し、に変更else ifして、2行目の後にをelsif追加する必要があります。この作業がすべて完了すると、次のようになります。endgrandtotal

print('What amount would you like to calculate tax for?  $')
subtotal = gets.to_i
taxrate = 0.078
tax = subtotal * taxrate
if tax > 0.0 then
  grandtotal = subtotal + tax
elsif tax < 0.0 then
  grandtotal = subtotal - tax
end
puts "Tax on $#{subtotal} is $#{tax}, so the grandtotal is $#{grandtotal}."

これはうまくいくようです。

于 2012-09-10T04:02:53.153 に答える