我的 while
循环有问题。循环直接从最后的 puts
显示,而不询问要猜测的数字 (adeviner)。
adeviner = 4
a = 0
while adeviner != 4
puts "Entrez votre chiffre"
a = gets.chomp.to_i
end
puts "vous avez devine le chiffre"
这是控制台返回的内容:
ruby test.rb
vous avez devine le chiffre
我希望控制台让我输入一个数字并告诉我这个数字是否正确。
最佳答案
我认为你不小心在循环中使用了错误的变量
adeviner = 4
a = 0
while a!= adeviner
puts "Entrez votre chiffre"
a = gets.chomp.to_i
end
puts "vous avez devine le chiffre"
上面的代码在循环中使用了一个变量。 a 最初设置为默认值 0(这不是我们试图猜测的数字)并且循环继续,直到用户正确输入“adeviner”值。
关于 ruby : the while loop does not work,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49884281/