如何在计算器程序中添加循环
我在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键而不输入任何文本。
PS:更好的用例运算符而不是多个if 。
PPS:所有代码,在循环添加和没有大小写操作符之后,将是:
把“你好,我的名字是加尔文计算器,我是一个计算器,可以做基本的function,如添加,减去,乘法和除法” 把“按a然后输入以启用我的服务” 直到gets.chomp ==“a” 把“我在等......” 结束 puts“选择你想要做的操作。+用于添加, - 用于减法,*用于乘法和/用于除法” 直到(which_operation = gets.chomp).empty? if which_operation ==“+” 把“你想要添加的第一个数字是什么” adding_first_number = gets.chomp.to_i 将“你要添加的第二个数字添加到#{adding_first_number}” adding_second_number = gets.chomp.to_i put“#{adding_first_number} +#{adding_second_number}是#{adding_first_number + adding_second_number}” elsif which_operation ==“ - ” 把“你要减去的第一个数字是什么” subtracting_first_number = gets.chomp.to_i 把“你要减去的数字是多少来减去#{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 ==“*” 把“你想要的第一个数字是多少” multiplying_first_number = gets.chomp.to_i 把“你想要的数字是多少#{multiplying_first_number}” multiplying_second_number = gets.chomp.to_i 把#{multiplying_first_number} *乘#{multiplying_second_number}是#{multiplying_first_number * multiplying_second_number}“ elsif which_operation ==“/” 把“你的分歧问题的第一个数字是什么?” divide_first_number = gets.chomp.to_i 提出“什么是除数?” divide_second_number = gets.chomp.to_i put“#{divide_first_number}除以#{divide_second_number}是#{dividing_first_number / dividing_second_number}” 结束 把“\ n再试一次:” 结束