如何在ruby计算器上添加循环

我在ruby中创建了一个简单的计算器,它运行正常。

puts "Hello, My name is Calvin The Calculator and I am a calculator that can do basic functions such as Adding, Subtracting, Multiplying and Dividing" puts "Press a and enter to enable my services" enable = gets.chomp if enable == "a" puts "Choose which operation you want to do. + for adding, - for subtraction, * for multiplication and / for division" else "Puts Im Waiting..." end which_operation = gets.chomp if which_operation == "+" puts "What is the first number you want to add" adding_first_number = gets.chomp.to_i puts "What is the second number you want to add to #{adding_first_number}" adding_second_number = gets.chomp.to_i puts "#{adding_first_number} + #{adding_second_number} is #{adding_first_number + adding_second_number}" else end if which_operation == "-" puts "What is the first number you want to subtract" subtracting_first_number = gets.chomp.to_i puts "What is the number you want to subtract from #{subtracting_first_number}" subtracting_second_number = gets.chomp.to_i puts "#{subtracting_first_number} - #{subtracting_second_number} is #{subtracting_first_number - subtracting_second_number}" else end if which_operation == "*" puts "What is the first number you want to multiple" multiplying_first_number = gets.chomp.to_i puts "What is the number you want to multiple #{multiplying_first_number} by" multiplying_second_number = gets.chomp.to_i puts "#{multiplying_first_number} * by #{multiplying_second_number} is #{multiplying_first_number * multiplying_second_number}" else end if which_operation == "/" puts "What is the first number to your divison question?" dividing_first_number = gets.chomp.to_i puts "What is the divisor?" dividing_second_number = gets.chomp.to_i puts "#{dividing_first_number} divided by #{dividing_second_number} is #{dividing_first_number / dividing_second_number}" else end 

我想知道如何为程序添加循环,以便它不会完全停止并继续运行。 任何帮助将不胜感激。

例如,这个循环:

 until (which_operation = gets.chomp).empty? if which_operation == "+" ... end if which_operation == "-" ... end if which_operation == "*" ... end if which_operation == "/" ... end end 

将工作,直到您按Enter键而不输入任何文本。 你最好使用case运算符而不是multiple if 。 在循环添加和没有case操作符之后,您的整个代码将是:

 puts "Hello, My name is Calvin The Calculator and I am a calculator that can do basic functions such as Adding, Subtracting, Multiplying and Dividing" puts "Press a and enter to enable my services" until gets.chomp == "a" puts "I'm Waiting..." end puts "Choose which operation you want to do. + for adding, - for subtraction, * for multiplication and / for division" until (which_operation = gets.chomp).empty? if which_operation == "+" puts "What is the first number you want to add" adding_first_number = gets.chomp.to_i puts "What is the second number you want to add to #{adding_first_number}" adding_second_number = gets.chomp.to_i puts "#{adding_first_number} + #{adding_second_number} is #{adding_first_number + adding_second_number}" elsif which_operation == "-" puts "What is the first number you want to subtract" subtracting_first_number = gets.chomp.to_i puts "What is the number you want to subtract from #{subtracting_first_number}" subtracting_second_number = gets.chomp.to_i puts "#{subtracting_first_number} - #{subtracting_second_number} is #{subtracting_first_number - subtracting_second_number}" elsif which_operation == "*" puts "What is the first number you want to multiple" multiplying_first_number = gets.chomp.to_i puts "What is the number you want to multiple #{multiplying_first_number} by" multiplying_second_number = gets.chomp.to_i puts "#{multiplying_first_number} * by #{multiplying_second_number} is #{multiplying_first_number * multiplying_second_number}" elsif which_operation == "/" puts "What is the first number to your divison question?" dividing_first_number = gets.chomp.to_i puts "What is the divisor?" dividing_second_number = gets.chomp.to_i puts "#{dividing_first_number} divided by #{dividing_second_number} is #{dividing_first_number / dividing_second_number}" end puts "\nLet's try again: " end 

将代码包装在while循环中。

 while input.eql?('y') || input.eql?('Y') puts 'If you want to continue press y/Y' input = gets.chomp end 

当你有多个if语句时尝试使用案例,这将使你的代码更清晰。