感谢您查看我的问题! :)
我是一个noob学习Ruby for Rails类(在SMC上),而且我坚持其中一个与循环和函数有关的练习作业。问题似乎是我无法弄清楚如何从我的函数中使用的变量中将正确的值返回到主程序。
这是目标:
创建一个Ruby程序,计算参加圣莫尼卡学院的学生的学费。为了获得完整的信用,你必须创建功能来解决这个问题与参数。此外,为了获得全额信贷,当用户输入无效数据值时,您必须接受并接受异常。
下表中总结了我希望您的程序执行的成本计算。
圣塔莫尼卡大学学生费(截至2014年秋季)
报名费
$ 46.00 /加州居民单位 $ 330.00 /单位为F1 /非居民
学生服务费 (AS贴纸费用可选,节省19.50美元) (身份证费用可选,节省13美元)
$ 47.50冬/夏 秋季/春季$ 50.50
停车贴花(可选)
$ 45.00冬/夏 $ 85.00秋/春
该节目对话描述了我正在寻找的节目。
SMC Fee Calculator
Enter number of units enrolled: 18
Is this Fall[0], Winter[1], Spring[2] or Summer[3] session: 0
Are you a state resident[0] or not[1]: 0
Want a parking decal? [y/n]: n
Want an AS sticker? [y/n]: n
Want an ID card? [y/n]: n
For Fall semester, your total fees are $ 846.00
这是我的程序,我遇到了麻烦(我已经注释了一些我不确定的代码):
# program that calculates student fees for those attending Santa Monica college
# I just can't seem to figure out how to calculate and print out the 'fees' variable correctly
require_relative 'unit5test'
puts "SMC Fee Calculator"
begin
print "Is this Fall[0], Winter[1], Spring[2] or Summer[3] session: "
semester = gets.chomp.to_i
print "Enter number of units enrolled: "
units = gets.chomp.to_i
print "Are you a state resident[0] or not[1]: "
resident = gets.chomp.to_i
print "Want a parking decal? [y/n]: "
decal = gets.chomp
print "Want an AS sticker? [y/n]: "
sticker = gets.chomp
print "Want an ID card? [y/n]: "
card = gets.chomp
fees = 0
unit_fee( units, resident, fees )
decal_fee( decal, semester, fees )
total_fees = services_fee( sticker, card, semester, fees )
# fees = '%.2f' % fees
puts "For ", session_type( semester ), " semester, your total fees are $ ", total_fees # don't know what to put here
# rescue
# puts "Sorry, you've entered an incorrect value!"
end
这些是我的函数,位于名为' unit5test.rb'的单独文件中:
# contains functions for the SMC Fee Calculator
def session_type ( semester )
if (semester == 0)
semester = "Fall"
elsif (semester == 1)
semester = "Winter"
elsif (semester == 2)
semester = "Spring"
elsif (semester == 3)
semester = "Summer"
end
return semester
end
def unit_fee ( units, resident, fees )
# if (semester == 0 or semester == 2)
if (units < 0 or units > 16)
raise "inappropriate number of units entered for semester"
end
# elsif (semester == 1 or semester == 3)
# if (units < 0 or units > 8)
# raise "inappropriate number of units entered for session"
# end
# end
if (resident == 0)
fees += (units * 46.00)
elsif (resident == 1)
fees += (units * 325.00)
end
return fees
end
def decal_fee ( decal, semester, fees )
if (semester == 0 or semester == 2)
if (decal == 'y')
fees += 85.00
end
elsif (semester == 1 or semester == 3)
if (decal == 'y')
fees += 45.00
end
end
return fees
end
def services_fee ( semester, sticker, card, fees )
if (semester == 0 or semester == 2)
fees += 50.50
if (sticker == 'n')
fees -= 19.50
end
if (card == 'n')
fees -= 13.00
end
elsif (semester == 1 or semester == 3)
fees += 47.50
if (sticker == 'n')
fees -= 19.50
end
if (card == 'n')
fees -= 13.00
end
end
return fees
end
我得到的错误输出:
SMC Fee Calculator
Is this Fall[0], Winter[1], Spring[2] or Summer[3] session: 0
Enter number of units enrolled: 12
Are you a state resident[0] or not[1]: 0
Want a parking decal? [y/n]: n
Want an AS sticker? [y/n]: n
Want an ID card? [y/n]: n
For
Fall
semester, your total fees are $
0
先谢谢你帮助我!尽管有困难,我喜欢学习Ruby! :)
答案 0 :(得分:1)
您的密码:
fees = 0
unit_fee( units, resident, fees )
decal_fee( decal, semester, fees )
total_fees = services_fee( sticker, card, semester, fees )
将其更改为:
fees = 0
fees+=unit_fee( units, resident, fees )
fees+=decal_fee( decal, semester, fees )
total_fees = services_fee( sticker, card, semester, fees )
当您将fees
传递给unit_fee
或decal_fee
时,只会传递fees
的值,因此它不会更改{{1}的原始值那是fees
。