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

问题描述 投票:-2回答:1

我用红宝石创建了一个计算器。我想知道如何把它放在一个循环中,所以我不必经常运行它。我是编程新手,所以请理解我只是想学习。我将不胜感激任何帮助。

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
ruby
1个回答
0
投票

例如: until (which_operation = gets.chomp).empty? if which_operation == "+" ... end if which_operation == "-" ... end if which_operation == "*" ... end if which_operation == "/" ... end end 此循环将一直有效,直到您按Enter键而不输入任何文本。 P.S。:更好的用例运算符而不是多个if。 P.P.S。:所有代码,在循环添加和没有大小写操作符之后,将是:

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
© www.soinside.com 2019 - 2024. All rights reserved.