如何在计算器程序中添加循环

时间:2018-10-21 23:42:26

标签: 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

1 个答案:

答案 0 :(得分:0)

例如:
//JS const input = document.querySelector('#input'), output = document.querySelector('#output'); button.addEventListener('change', () => { output.innerHTML = input.value; })
该循环将一直有效,直到您按Enter键而不输入任何文本为止。

附注:最好使用 case 运算符,而不要使用多个 if

P.P.S .:添加循环后且没有 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