Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby初学者:代码只重复2次_Ruby - Fatal编程技术网

Ruby初学者:代码只重复2次

Ruby初学者:代码只重复2次,ruby,Ruby,嘿,我创建的代码只重复了2次。 在我为“continue\u question”方法第二次键入“y”之后,代码只会停止 def greeting puts "Hello! Please type your name: " name = gets.chomp.capitalize puts "It is nice to meet you #{name}. I am a simple calculator application." puts "I can add

嘿,我创建的代码只重复了2次。 在我为“continue\u question”方法第二次键入“y”之后,代码只会停止

def greeting  
  puts "Hello! Please type your name: "  
  name = gets.chomp.capitalize  

  puts "It is nice to meet you #{name}.  I am a simple calculator application."  
  puts "I can add, subtract, multiply, and divide."  
end  

greeting  

def calculator  

    puts "First number: "  
    @n1 = gets.chomp.to_i  
    puts "Secons number: "  
    @n2 = gets.chomp.to_i  

def calculation
  puts "Type 1 to add, 2 to subtract, 3 to multiply, or 4 to divide two numbers: "
  operation_selection = gets.chomp.to_i  

    if operation_selection == 1  
      @result = @n1 + @n2  
    elsif operation_selection == 2  
      @result = @n1 - @n2  
    elsif operation_selection == 3  
      @result = @n1 * @n2  
    elsif operation_selection == 4  
      @result = @n1 / @n2  
    else  
      puts "Something went wrong!"  
      calculation  
    end  
end  

calculation  

puts "Your Result is #{@result}"  
end  

calculator  

def continue_question  
puts "Do you want to continue? (y/n)"  
continue = gets.chomp.to_s  

if continue == "y"  
  calculator  
  elsif continue == "n"  
  puts "Bye!"  
else  
  puts "What?"  
  continue_question  
end  
end  

continue_question 

问题是
continue\u question
只在代码末尾执行一次,但需要循环直到用户退出(即键入
n

因此,只需在
continue\u question
中添加一个循环,例如:

def continue_question 
  continue = "y" 

  until continue == "n" do
    puts "Do you want to continue? (y/n)"  
    continue = gets.chomp.to_s  

    if continue == "y"  
      calculator  
    elsif continue == "n"  
      puts "Bye!"  
    else  
      puts "What?"  
    end
  end
end 

您的代码不会重复2次,而是重复一次

原因是在您的
continue\u question
方法中,您没有告诉它再次重复:

def continue_question
  puts "Do you want to continue? (y/n)"
  continue = gets.chomp.to_s

  if continue == "y"
    calculator # <-- This causes it it repeat ONCE!
  elsif continue == "n"
    puts "Bye!"
  else
    puts "What?"
    continue_question
  end
end
嘿,我创建的代码只重复了2次

我想你误解了以下几点:

if continue == "y"  
  calculator  
  elsif continue == "n"  
  puts "Bye!"  

当您调用上述函数
计算器
时,您仍在执行
continue\u question
函数。因此,当
计算器
完成执行时,
continue\u question
也将完成,程序将停止。对于想要的结果,您可以尝试使用.< /p>,您也可以考虑使用<代码>而或<代码>循环< /代码>,而不是像这样递归调用方法。然而,这是一个更大的重构,可能超出了您最初问题的范围。
if continue == "y"  
  calculator  
  elsif continue == "n"  
  puts "Bye!"