我正在使用RUBY
puts "ENTERING STAGE~1"
puts ""
puts "First_name: "
first_name = gets.chomp
puts ""
puts "You are #{first_name}. Is info correct? [y/n]"
true_false = gets.chomp
if true_false == "y"
puts ""
puts "Good!"
puts ""
puts "ENTERING STAGE~2"
else true_false == "n"
puts "Please correct the information." #jump: puts "First name: "
end
puts ""
puts "Last name: "
last_name = gets.chomp
puts ""
puts "You are #{first_name} #{last_name}."
我想在"请更正信息…"之后:程序跳回first_name=gets.chomp并继续。如何?
您要查找的是一个循环结构,如while
、until
或for
循环。一个简单的例子:
# We set a boolean value that tells us if our work is done
finished = false
# We use an until loop which will keep repeating until finished is set to true
until finished do
puts "First name: "
first_name = gets.chomp
puts "You are #{first_name}. Is info correct? [y/n]"
response = gets.chomp
if response == "y"
# If we receive a y as input, then we can set finished to true to exit the loop
finished = true
end
end
不过,这个代码有点乱,您必须为后面的第二个名称重复它。相反,可以考虑将读取逻辑提取到方法中。
def read_input(prompt_text)
finished = false
result = ""
until finished do
puts prompt_text
result = gets.chomp
puts "You entered: #{result}. Is this correct? [y/n]"
response = gets.chomp
if response == "y"
finished = true
end
end
return result
end
然后,您可以这样调用函数:
first_name = read_input("First name: ")
last_name = read_input("Last name: ")
puts "You are #{first_name} #{last_name}"
使用loop do
:只需我的两美分
loop do
puts "First_name: "
first_name = gets.chomp
puts "You are #{first_name}. Is info correct? [y/n]"
true_false = gets.chomp
break if true_false == "y"
puts "Please correct the information."
end
puts "Good!"
puts "ENTERING STAGE~2"
CCD_ 5退出循环并且仅当条件CCD_ 6是CCD_。我会把剩下的放在圈外。
您可以使用循环构造。例如
true_false = false
while (not true_false)
puts "First_name: "
first_name = gets.chomp
puts ""
puts "You are #{first_name}. Is info correct? [y/n]"
true_false = gets.chomp
if true_false == "y"
puts ""
puts "Good!"
puts ""
puts "ENTERING STAGE~2"
else true_false == "n"
puts "Please correct the information." #jump: puts "First name: "
end
end
如果您也想验证姓氏,您可以创建一个方法来获得名称
def get_name (type)
true_false = 'n'
while true_false != 'y'
puts "#{type} name: "
name = gets.chomp
puts "Your #{type} name is: #{name}. Is info correct? [y/n]"
true_false = gets.chomp
if true_false == 'n'
puts "Please correct the information."
end
end
name
end
def runner
puts "ENTERING STAGE~1nn"
first_name = get_name("First")
puts "nGood!n"
puts "ENTERING STAGE~2nn"
last_name = get_name("Last")
puts "nYou are #{first_name} #{last_name}."
end
runner
默认情况下,ruby没有Goto
语句或类似的语句,但您可以使用递归和函数来实现这一点:
def foo()
puts "First_name: "
first_name = gets.chomp
puts
puts "You are #{first_name}. Is info correct? [y/n]"
true_false = gets.chomp
if true_false == "y"
puts
puts "Good!"
puts
puts "ENTERING STAGE~2"
first_name
else true_false == "n"
puts "Please correct the information." #jump: puts "First name: "
foo()
end
end
puts "ENTERING STAGE~1"
puts
first_name = foo()
puts
puts "Last name: "
last_name = gets.chomp
puts
puts "You are #{first_name} #{last_name}."
类似的原则适用于您可能需要相同功能的任何时候。您可以看到,我在其内部调用foo()
,这被称为递归。有了它,您可以重复相同的代码,直到用户得到正确的输入。