Ruby ';内的减法运算的语法错误;如果';块
我得到以下错误:Ruby ';内的减法运算的语法错误;如果';块,ruby,syntax-error,if-statement,Ruby,Syntax Error,If Statement,我得到以下错误: /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) grand
/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}."
print('您希望计算多少税额?$))
小计=gets.to_i
税率=0.078
税=小计*税率
如果(税>0.0)
总计=$#{小计+税金}
否则,如果(税<0.0)
总计=$#{小计-税}
将“对$#{subtotal}征税的金额为$#{Tax},因此总金额为$#{grandtotal}。”
我想知道我是否需要以不同的方式将小计
设置为一个值,或者我可以做些什么来修复我的程序
在第10行,我还收到了一个意外的$end错误。这里的语法有一些错误 首先,在将变量插入带引号的字符串中时,只需要使用
$#{blah}
语法(并且仅有效!)。当您只是执行计算时,您可以简单地说:
grandtotal = subtotal + tax
您还需要在两行if
的末尾添加then
,将else if
更改为elsif
,并在第二行grandtotal
之后添加end
。完成这些工作后,您应该:
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}."
print('您希望计算多少税额?$))
小计=gets.to_i
税率=0.078
税=小计*税率
如果税收>0.0,则
总计=小计+税金
如果税收<0.0,则
总计=小计-税金
结束
将“对$#{subtotal}征税的金额为$#{Tax},因此总金额为$#{grandtotal}。”
这似乎有效。谢谢,伙计。我不知道美元的废话是给弦的你能解释一下为什么它的处理方式不同吗?我在第10行仍然收到一个错误。我将把它添加到问题中。它说,
意外的$end
$#{blah}
根本不是有效的语法,你可以稍微掩饰一下。@Wolfpack'08是的,但是$
只是字符串中的一个字符,没有什么特别的。与“f#{subtotal}”
没有区别。