如果我給出的輸入爲1或2,無論程序如何默認。嘗試將輸入與「1」和1兩者進行比較。同樣的結果。 我的第一個Ruby程序,plz對於天真的藉口。程序沒有在Ruby中的if-else塊中輸入
$choice
def getInfo
puts "Info"
end
def getMoreInfo
puts "MoreInfo"
end
def switch
if $choice == "1" #intentionally in ""
getInfo
elsif $choice == 2 #intentionally without ""
getMoreInfo
else
puts "default"
end
end
def callMainMenu
puts "Choose the operation:"
puts "[1] Get some Info"
puts "[2] Get some moreInfo"
$choice=gets
$choice.chomp
end
callMainMenu
switch
非常感謝你:)大家都在努力......選擇「bdon」作爲答案,因爲它是第一個回覆!謝謝你們!! – 2012-02-22 22:03:20
作爲ruby風格的問題...只是不使用全局'$'。 – DGM 2012-02-22 23:15:21