2017-07-12 3 views
0

Hey der Code, den ich erstellt habe, wiederholt nur 2 mal. Nachdem ich das zweite Mal "y" für die "continue_question" -Methode eingegeben habe, stoppt der Code nur noch.Ruby Anfänger: Code wiederholt nur 2 mal

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 

Antwort

0

Ihr Code nicht 2-mal wiederholt sich, es wiederholt einmal.

Der Grund ist, weil in Ihrem continue_question Methode, Sie es nicht sagen, noch einmal wiederholen:

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 

Eine schnelle Lösung ist unterhalb dieser Linie die continue_question Methode erneut aufrufen, um sich selbst rekursiv wiederholen:

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

    if continue == "y" 
    calculator 
    continue_question # <-- Add this to repeat indefinitely 
    elsif continue == "n" 
    puts "Bye!" 
    else 
    puts "What?" 
    continue_question 
    end 
end 
+0

Sie könnten auch 'while' oder' loop' verwenden, anstatt die Methoden rekursiv wie folgt aufzurufen. Das ist jedoch ein größerer Refactor, der wahrscheinlich über Ihre ursprüngliche Frage hinausgeht. –

0

Das Problem ist, dass continue_question nur einmal ausgeführt wird, am Ende des Codes, aber Sie müssen bis zum User-Exits in einer Schleife (d Typen n).

So einfach eine Schleife in continue_question hinzufügen, zum Beispiel:

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 
0

Hey der Code, den ich erstellt habe, wird nur zweimal wiederholt.

Ich glaube, Sie mißverstehen die folgenden:

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

Wenn Sie die obige Funktion calculator nennen, Sie ausführen noch die continue_question Funktion. Wenn die calculator beendet wird, wird continue_question ebenfalls beendet und das Programm wird beendet. Für das gewünschte Ergebnis können Sie versuchen, eine loop verwenden.