Ruby 循环使用用户输入



>我正在编写一个程序,它获取员工的工资和工作时间,并输出每个员工的年薪,哪个员工工作最多,哪个员工赚得最多,等等。

这是我到目前为止所拥有的:

puts "Name of your employee"
employee_1 = gets.chomp!
puts "How much does #{employee_1} make per hour?"
employee_1_wage = gets.chomp!
puts "How many hours does #{employee_1} work per day?"
employee_1_hours = gets.chomp!
puts "Do you have another employee you would like to enter? If 
yes, press ENTER"
pause = STDIN.gets
puts "Name of your employee"
employee_2 = gets.chomp!
puts "How much does #{employee_2} make per hour?"
employee_2_wage = gets.chomp!
puts "How many hours does #{employee_2} work per day?"
employee_2_hours = gets.chomp!
puts "Do you have another employee you would like to enter? If 
yes, press ENTER"
pause = STDIN.gets

我想让它成为一个循环,但我什至不知道从哪里开始。循环如何自动将第一个输入分配为employee_1,第二个输入分配为employee_1_wage,依此类推?那么对于第二个循环作为employee_2,等等?因为稍后我想参考这些来计算每个员工的年薪等。

这是一个奇怪的案例。因为,首先,你的循环本能是好的。但是你处于一个尴尬的境地,因为你还没有持久化数据,这总是使这些入门级Ruby示例有点难以学习。至少在我看来。

但是,这个实际上可能很有趣,所以我将尝试为您提供一个我希望有意义的解决方案。披露:这将很长,你可能不明白发生了什么。我会尽力逐行分解,但你可能仍然对我答案的某些方面感到沮丧。因为在现实中,也许有一些更简单的东西可以理解。但是如果你遵循我写的东西,玩它,并真正尝试理解它,我认为你将处于一个非常好的位置,可以理解Ruby约定是如何工作的。要有耐心,要敞开心扉。

将我在底部添加的内容复制到您的文本编辑器中并保存。通过运行以下命令运行文件:ruby whatever_you_named_the_file.rb

运行几次,输入一个员工,然后输入多个员工的数据。看看你如何让它打破。然后查看周围的代码更改内容,看看它如何更改输出。

为了解决您的观点"循环如何自动将输入分配为employee_1?答案是,不会。循环必须与它分配的变量无关。这意味着所有的任务都只是employee,循环的每个"实例"将只处理一个员工。

无论如何,这是完整的代码。在此之下,我将尝试分解每个部分并在整个过程中添加注释:

这是完整的代码,你可以把它放在编辑器中来弄乱:

$employees_array = []
def get_employee_data
puts "Name of your employee"
employee = gets.chomp!
puts "How much does #{employee} make per hour?"
employee_wage = gets.chomp!
puts "How many hours does #{employee} work per day?"
employee_hours = gets.chomp!
add_employee(employee, employee_wage, employee_hours)
puts "Do you have another employee you would like to enter?"
response = gets.chomp!
if response.downcase == "no"
display_employee_salaries
else
get_employee_data
end
end
def add_employee(employee, employee_wage, employee_hours)
employee_hash = {
name: employee,
wage: employee_wage.to_i,
hours: employee_hours.to_i,
}
$employees_array << employee_hash
end
def display_employee_salaries
$employees_array.each do |employee|
puts "Employee #{employee[:name]} makes $#{employee[:wage]} per hour and worked #{employee[:hours]}. That means their annual salary is: $#{ employee_salary(employee) } "
end
end
def employee_salary(employee)
employee[:wage] * employee[:hours] * 261 # 261 is the number of working days per year
end
get_employee_data

现在,尝试分解一下:

$employees_array = [] # This is a global variable that will simulate a database for us so we can keep track of our employees.
def get_employee_data #the first half of this is largely similar to what you have. It will prompt the user for information.
puts "Name of your employee"
employee = gets.chomp!
puts "How much does #{employee} make per hour?"
employee_wage = gets.chomp!
puts "How many hours does #{employee} work per day?"
employee_hours = gets.chomp!
# Now we should hand this data off to another method, so that we can continue our loop and get more employees.
add_employee(employee, employee_wage, employee_hours) # this is a call to a method we define below. It passes all of the information gathered in the first loop so we can add another employee.
puts "Do you have another employee you would like to enter?"
response = gets.chomp!
# Now we decide if we want to loop again or return the output based on what the user says
if response.downcase == "no" #if the user says no, that's when we know to break the loop and calculate our salary data.
display_employee_salaries # this is another method that we've defined below, it is the final step to the loop.
else
get_employee_data # if a user says *anything* other than "no", we call this method, which will restart this loop. This might be confusing, that's because this is known as recursion--which can be a difficult concept to grasp. Be patient.
end
end

这是我们需要定义的第一个新方法。这样做是创建我们为一名员工收集的所有数据的哈希值,它包含基于用户在上一种方法(上图)中提供给我们的"姓名"、"工资"和"小时数"。

def add_employee(employee, employee_wage, employee_hours)
employee_hash = {
name: employee,
wage: employee_wage.to_i,
hours: employee_hours.to_i,
}
end

还记得文件顶部的全局变量吗?现在我们只需将此哈希添加到全局数组中。

$employees_array << employee_hash

第一次运行时,数组将为空,即:[]

第二次运行时,数组内部将有一个employee_hash:[employee_hash]

如果输入了另一个员工,则再次执行此行,因此数组将具有[employee_hash, employee_hash]

这可能发生在您输入的尽可能多的员工身上。因此,如果您输入员工 5 次,数组将如下所示:[employee_hash, employee_hash, employee_hash, employee_hash, employee_hash]

您可能想知道"这些不都一样吗?它怎么知道区别?变量名称相同,哈希的内容不同。它不知道区别。但并非必须如此。


我们定义的第二种方法如下。如果用户在被问及"您是否有其他员工要进入"时回答"否",则会调用此名称。

此方法的责任是在输入所有员工后打印出员工工资。

为此,我们遍历(循环)全局员工数组,对于该数组中的每个条目,我们执行一个 put 命令来打印出我们的数据。当我们调用".each"时,就会发生这种情况。它说,"对于这个数组中的每个项目,称其为"员工",并执行这些操作"。一旦数组的项用完,循环结束,程序退出。

def display_employee_salaries
$employees_array.each do |employee|
puts "Employee #{employee[:name]} makes $#{employee[:wage]} per hour and worked #{employee[:hours]}. That means their annual salary is: $#{ employee_salary(employee) } "
end
end

需要注意的几件事:如果您不熟悉哈希查找 - 这是一个示例:employee[:name]这是在做什么,它是在一个名为"员工"的哈希中寻找"名称"键。如果有"name"键,那么它将向我们返回值。因此,如果"员工"是一个看起来像这样的哈希:

{ name: "ActiveModel_Dirty", wage: "40", hours: "8" }

然后employee[:name]将返回"ActiveModel_Dirty",employee[:wage]将返回"40",依此类推。

您可能想知道:"嘿,我们之前称该哈希employee_hash,为什么我们突然将其称为"员工"?问得好。在下面的.each do之后,您将看到|employee|。这里的"员工"可以是您想要的任何内容。这是我们引用当前项目的方式,我们只是在这个循环中迭代,它与参考点以外的任何东西无关。

最后,您会注意到在 put 命令结束时调用 employee_salary。这是在调用我们在下面定义的另一种方法。


最后一种方法是用来做数学的——根据用户给我们的关于一个员工的信息来计算年薪。它接收一个雇员的实例作为参数(这就是(雇员))。

它将计算工资乘以员工工时,再乘以一年的工作日。一旦它执行了这个计算,Ruby默认将返回它想出的任何内容。然后,我们在上面的"puts"语句中将其打印到控制台。

此方法返回一个数字,该数字是年薪的值

def employee_salary(employee)
employee[:wage] * employee[:hours] * 261 # 261 is the number of working days per year
end

我真的希望这对你有所帮助,我希望它不会让人不知所措。没有人说你需要知道这一切现在是如何工作的。尽可能多地处理,然后在需要时返回。

像这样:

# an array to hold as many employees as you wish
employees = []
loop do 
# a hash to store data of one employee
employee = {}
puts "Name of your employee"
employee[:name] = gets.chomp!
puts "How much does #{employee[:name]} make per hour?"
employee[:wage] = gets.chomp!
puts "How many hours does #{employee[:name]} work per day?"
employee[:hours] = gets.chomp!
# adding the employee to the array
employees << employee
puts "Do you have another employee you would like to enter? If 
yes, press ENTER. If no, press 'x'"
command = STDIN.gets
break if command.chomp! == "x"
end

现在,您拥有一系列员工,可以对他们做任何您想做的事情。

这是一个经过测试(基本)和工作的简单入门脚本,您可以使用完整的提示和响应列表来充实它,显然您自己的全局(内核)方法"进程"会更加详细:

#!/usr/bin/env ruby
# filename: prompted_loop_example.rb
def process( emp, pay )
pp emp, pay
end # process
loop do
puts "Employee name (<Enter> or Ctrl/D to quit): "
emp1 = STDIN.gets.chomp!
break if emp1 == "" || emp1.nil?
puts "Pay scale: "
pay1 = STDIN.gets.chomp!
process( emp1, pay1 )
end
exit 0

相关内容

  • 没有找到相关文章

最新更新