这有效:
#Loop naming accounts====================
num = 0 #<--Generic counting variable
loop do
print ("\nEnter account name or press 'q': > ")
names[num] = gets.chomp
if names[num] == "q"
break
end
puts ("The account name is #{names[num]}.")
num += 1
end
这里显而易见的问题是我不想要&#34; q&#34;成为其中一个帐户。
这不起作用:
#Loop naming accounts====================
num = 0 #<--Generic counting variable
loop do
print ("\nWould you like to add an account? [1 - yes] [2 - no]: > ")
varr = nil
varr = gets.chomp.to_i
if varr == 2
break
end
names[num] = gets.chomp
puts ("The account name is #{names[num]}.")
num += 1
end
这会将我的终端发送到完全黑屏。问题:
为什么第一个例子用于突破循环,但第二个例子不会?
为什么第二个例子奇怪地打破了一个完全空白的屏幕,而不是抛出错误等?
如何正确执行此操作?
谢谢!
答案 0 :(得分:0)
num = 0 #<--Generic counting variable
names = [] # Collection for names
loop do
print ("\nWould you like to add an account? [1 - yes] [2 - no]: > ")
varr = gets.chomp.to_i
break if varr == 2
puts "Enter name"
names[num] = gets.chomp
puts ("The account name is #{names[num]}.")
num += 1
end