首先,我是红宝石的新手,所以请温柔嘿嘿。
我有一个学校项目,其中的任务是计算项目的总成本。在我的代码中,用户给变量一些输入。稍后,我想显示所有这些输入的总和。 (输入数字) 所以基本上我需要添加它们,它需要给我一个总和。
我该怎么做?
希望你有惊人的人可以帮助我:)
这是我的代码:
file = File.new("testing.txt", "a")
class Project
def call_acc
prompt = "> "
puts
puts "What is the salary for an accountant?"
print prompt
while @accountant = gets.chomp
if !/\A\d+\z/.match(@accountant)
puts
puts "Error: Only use numbers, please try again"
print prompt
else
break
end
end
end
def call_dev
prompt = "> "
puts
puts "What is the salary for an developer?"
print prompt
while @developer = gets.chomp
if !/\A\d+\z/.match(@developer)
puts
puts "Error: Only use numbers, please try again"
print prompt
else
break
end
end
end
def call_mana
prompt = "> "
puts
puts "What is the salary for the top management?"
print prompt
while @management = gets.chomp
if !/\A\d+\z/.match(@management)
puts
puts "Error: Only use numbers, please try again"
print prompt
else
break
end
end
end
def call_office
prompt = "> "
puts
puts "What is the total office rent for the project?"
print prompt
while @office = gets.chomp
if !/\A\d+\z/.match(@office)
puts
puts "Error: Only use numbers, please try again"
print prompt
else
break
end
end
end
def call_lunch
prompt = "> "
puts
puts "What is the daily cost for lunch per person?"
print prompt
while @lunch = gets.chomp
if !/\A\d+\z/.match(@lunch)
puts
puts "Error: Only use numbers, please try again"
print prompt
else
break
end
end
end
def call_utilites
prompt = "> "
puts
puts "What is the total cost for utilites (internet, subscriptions etc)?"
print prompt
while @utilites = gets.chomp
if !/\A\d+\z/.match(@utilites)
puts
puts "Error: Only use numbers, please try again"
print prompt
else
break
end
end
end
def call_show
prompt = "> "
puts "____________________________________"
puts
puts "Is this information correct?"
puts
puts "Accountant salary (per hour): #{@accountant}kr\nDevelepor salary (per hour): #{@developer}kr\nTop management salary (per hour): #{@management}kr"
puts
puts "Total rent cost of office space: #{@office}kr\nLunch per person per day: #{@lunch}kr\nTotal cost of utilites #{@utilites}kr"
puts
puts "____________________________________"
puts "Yes or No"
print prompt
while user_imput = gets.chomp.upcase
case user_imput
when "YES"
file = File.new("testing.txt", "a")
file.puts("Account salary: #{@accountant}kr\nDeveloper selary: #{@developer}kr\nTop management salary #{@management}kr\nTotal rent cost: #{@office}kr\nLunch per person #{@lunch}kr\nUtilites cost #{@utilites}kr")
file.close
puts
puts "The information has now been stored"
puts "____________________________________"
break
when "NO"
puts
puts "The information has not been stored. Exiting application"
puts "____________________________________"
abort
else
puts
puts "Please either write Yes or No"
print prompt
end
end
end
def call_total
prompt = "> "
puts
puts "________Your information_______"
puts
puts "Accountant salary (per hour): #{@accountant}kr\nDevelepor salary (per hour): #{@developer}kr\nTop management salary (per hour): #{@management}kr"
puts
puts "Total rent cost of office space: #{@office}kr\nLunch per person per day: #{@lunch}kr\nTotal cost of utilites #{@utilites}kr"
puts
puts "________Total cost of project________"
puts
puts ?????????@accountant + @developer??????????????+
puts
end
project = Project.new
require 'io/console'
select = 0
prompt = "> "
puts
puts
puts "Welcome to KEA"
puts "____________________________________"
loop do(select != 7)
puts
puts "Press 1 to calculate"
puts "____________________________________"
select = STDIN.getch.to_i
if (select == 1)
project.call_acc
project.call_dev
project.call_mana
project.call_office
project.call_lunch
project.call_utilites
project.call_show
project.call_total
break
else
puts
puts "Invalid input. Please try again"
puts "____________________________________"
end
end
end
答案 0 :(得分:0)
回答标题中所述的问题:
numbers =
loop.inject([]) do |acc, _|
val = gets.to_i
break acc unless val > 0
acc << val
end
numbers.reduce(:+)