Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何对RUBY中的变量值求和_Ruby - Fatal编程技术网

如何对RUBY中的变量值求和

如何对RUBY中的变量值求和,ruby,Ruby,首先,我是ruby的新手,所以请温柔呵呵 我有一个学校项目,任务是计算一个项目的总成本。在我的代码中,用户给变量一些输入。稍后,我想显示所有这些输入的总和。(输入为数字) 所以基本上我需要把它们加起来,它需要给我一个它们的总和 我该怎么做 希望你们这些了不起的人能帮助我:) 这是我的密码: file = File.new("testing.txt", "a") class Project def call_acc prompt = "> " puts

首先,我是ruby的新手,所以请温柔呵呵

我有一个学校项目,任务是计算一个项目的总成本。在我的代码中,用户给变量一些输入。稍后,我想显示所有这些输入的总和。(输入为数字) 所以基本上我需要把它们加起来,它需要给我一个它们的总和

我该怎么做

希望你们这些了不起的人能帮助我:)

这是我的密码:

    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

回答标题中所述的问题:

numbers =
  loop.inject([]) do |acc, _|
    val = gets.to_i
    break acc unless val > 0
    acc << val
  end

numbers.reduce(:+)
数字=
循环注入([])do | acc_|
val=get.to_i
除非val>0,否则断开acc

acc运行代码时会发生什么?您是否收到错误或意外输出?“用户正在给变量一些输入”-您能否显示从用户收集输入并将其分配给变量的代码?什么是
@accounter
?什么是
@developer
?我们不知道如何解决您的问题,如果我们不知道如何重现它的话。您还没有展示用户是如何输入的。我猜,这是初学者最常见的错误,您正在做的是
get.chomp
,它将数据存储为字符串。您需要通过调用
.to_i
,将此值转换为一个数字,例如一个
整数