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'返回一個整數,從而產生正確的輸出。在整數的末尾沒有添加直覺/'幾乎不需要'的換行符,就像在字符串中一樣。 –