0
这是我的代码:我的打印/获取或输出错误的类型?
print('What amount would you like to calculate tax for? ')
subtotal = gets.chomp
taxrate = 0.078
tax = subtotal * taxrate
puts "Tax on $#{subtotal} is $#{tax}, so the grand total is $#{subtotal + tax}."
首页输出:What amount would you like to calculate tax for?
输入:100
。
最终输出:Tax on $100 is $, so the grand total is $100.
我相信我应该得到的$7.79999999
税率和总计107.7999999
。我希望通过执行诸如从输入中剥离$,如果用户错误地输入$,并四舍五入到最接近的分数来使代码更好一些。首先,我需要理解为什么我没有得到任何输出或补充,但是,对吗?
@ Wolfpack'08:你在说什么? –
基本上,'taxrate = gets.to_i'返回一个整数,从而产生正确的输出。在整数的末尾没有添加直觉/'几乎不需要'的换行符,就像在字符串中一样。 –