Как добавить цикл в программу калькулятора - PullRequest
0 голосов
/ 22 октября 2018

Я создал калькулятор в рубине.Мне интересно, как поставить это в цикл, чтобы мне не приходилось постоянно его запускать.Я новичок в программировании, поэтому, пожалуйста, поймите, я просто пытаюсь учиться.Буду признателен за любую помощь.

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 голосов
/ 22 октября 2018

Например:
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: лучше использовать оператор case вместократный , если .

PPS: весь ваш код после добавления цикла и без оператора 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
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...