如何使代码跳转到整个代码中的任何位置?

我正在使用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并进一步。 如何?

默认情况下,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}." 

类似的原则适用于您可能需要相同function的任何时间。 你可以看到我在自己内部调用foo() ,这叫做递归 。 有了它,您可以重复相同的代码,直到用户获得正确的输入。

您正在寻找的是一个循环结构,例如whileuntilfor循环。 一个简单的例子:

 # 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 ay 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" 

break退出循环并仅在条件true_false == "y"true 。 我会将其余部分保留在循环之外。

您可以使用循环结构。 例如

 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 

如果您还想validation姓氏,可以创建一个获取名称的方法

 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~1\n\n" first_name = get_name("First") puts "\nGood!\n" puts "ENTERING STAGE~2\n\n" last_name = get_name("Last") puts "\nYou are #{first_name} #{last_name}." end runner