我想用下面的代碼重寫下面的代碼,但我被卡住了。ruby程序基本設計說明
def ask question
good_answer = false
while (not good_answer)
puts question
reply = gets.chomp.downcase
if (reply == 'yes' or reply =='no')
good_answer = true
if reply == 'yes'
answer = true
else
answer = false
end
else
puts 'Please answer "yes" or "no"'
end
end
answer
end
替換代碼:
def ask question
puts question
reply = gets.chomp
if (reply == 'yes' or reply == 'no')
puts reply.capitalize
else
puts 'Please enter "yes" or "no"'
#jump the code to like 2 (but how?)- use while reply != empty & comment the below lines
puts question
reply = gets.chomp
end
end
我要跳轉到程序的主要部分是沒有任何跳轉,跳轉或我可以調用方法,該方法裏面?
Ruby通常是用兩個空格縮進的,而不是四個。 – 2012-01-04 21:48:04