gpt4 book ai didi

ruby - Ruby用用户输入循环

转载 作者:数据小太阳 更新时间:2023-10-29 08:25:56 33 4
gpt4 key购买 nike

我正在写一个程序,它获取员工的工资和工作时间,并输出每个员工的年薪,哪个员工工作得最多,哪个员工工作得最多,等等。
以下是我目前掌握的情况:

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
运行几次,输入一个雇员,然后输入多个雇员的数据。看看你怎么能把它弄坏。然后看看代码的变化,看看它是如何改变输出的。
要解决“循环如何自动将输入分配为employee_1”这一问题?答案是,不会的。循环必须对它分配的变量不可知。这意味着所有的任务都将简单地分配到ruby whatever_you_named_the_file.rb,循环的每个“实例”只处理一个雇员。
总之,这是完整的代码。在下面,我将尝试分解每个部分并在整个过程中添加注释:
下面是您可以在编辑器中处理的完整代码:

$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\u哈希: employee
如果输入了另一个雇员,则该行将再次执行,因此数组将具有 [employee_hash]
这可能发生在你进入的任何员工身上。因此,如果输入一个雇员5次,数组将如下所示: [employee_hash, employee_hash]
你可能在想“这些不都一样吗?它怎么知道区别呢?”变量名相同,散列的内容不同。不知道有什么区别。但没必要。
我们定义的第二种方法如下。如果用户在被问到“是否有其他要输入的员工”时回答“否”,则会调用此函数。
此方法的职责是在输入所有员工后打印出员工工资。
为此,我们遍历(循环)雇员的全局数组,并对该数组中的每个条目执行puts命令来打印数据。当我们叫“每一个”的时候就会发生这种情况。这是说,“对于这个数组中的每个项,称它为“employee”,然后执行这些操作”。一旦数组的项用完,循环结束,程序退出。
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_hash, employee_hash, employee_hash, employee_hash, employee_hash]
这是在一个叫做“employee”的散列中寻找一个“name”键。如果有一个“name”键,那么它将返回给我们值。所以,如果“employee”是这样的散列:
employee[:name]
然后 { name: "ActiveModel_Dirty", wage: "40", hours: "8" }将返回“activemodel_dirty”, employee[:name]将返回“40”,以此类推。
你可能会想:“嘿,我们刚才叫那个散列雇员,为什么我们突然把它称为“雇员”?好问题。在下面的 employee[:wage]之后,您将看到 .each do。雇员“这里可以是你想要的任何东西。这就是我们引用当前项的方式,我们只在循环中迭代,它与引用点无关。
最后,您将注意到在puts命令的末尾有一个对employee_salary的调用。这是调用我们在下面定义的另一个方法。
最后一种方法用于计算——根据用户提供给我们的员工信息计算年薪。它接收一个雇员的实例作为参数(这就是(雇员)是什么)。
它将计算工资乘以员工工时乘以一年中的工作日。一旦它执行了这个计算,ruby就会默认地返回它所得到的结果。然后在上面的“puts”语句中打印到控制台。
此方法返回一个数字,该数字是年薪的值
def employee_salary(employee)
employee[:wage] * employee[:hours] * 261 # 261 is the number of working days per year
end

我真的希望这能帮到你,我希望这不是压倒性的。没有人说你现在需要知道这一切是如何运作的。尽可能多的处理,如果需要的话再回来。

关于ruby - Ruby用用户输入循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49806062/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com