繁体   English   中英

Ruby-循环使用gets.chomp进行计算

[英]Ruby - calculations with gets.chomp in a loop

我是Ruby的新手。 我一直在尝试找到一种使用用户输入进行计算的方法。 我想做两件事:

  1. 输出哪个朋友的恐龙或水母最多。
  2. 输出哪个朋友的恐龙和水母加起来最多。

到目前为止,我只有这个循环要求用户输入:

f = "yes"

while f == "yes"
print "Enter your name: "
n = gets.chomp.to_f
print "Enter the number of dinosaurs you have: "
d = gets.chomp.to_f
print "Enter the number of jellyfish you have: "
j = gets.chomp.to_f
print "Another friend? (yes/no)"
f = gets.chomp
end

任何帮助表示赞赏。 非常感谢!

更新:非常感谢您的帮助。 我意识到我不够具体。 我会在这里尝试澄清。

我想要的输出看起来像这样(〜〜中的内容是用户输入):

Enter your name: ~ Bob~
Enter the number of dinosaur you have: ~3~
Enter the number of jellyfish you have: ~6~
Another friend? (yes/no) ~yes~
Enter your name: ~ Sally~
Enter the number of dinosaur you have: ~2~
Enter the number of jellyfish you have: ~8~
Another friend? (yes/no) ~no~
Friend who has the most dinosaurs: Bob
Friend who has the most jellyfish: Sally
Friend who has the most dinosaurs and jellyfish combined: Sally

到目前为止,我编写的代码仅使我进入“另一个朋友?(是/否)”,但是我不确定如何要求Ruby输出我想要的最后三行。 请大家对此有所启发吗?

非常感谢你!

更多更新:感谢您的所有帮助! 用Hash和Array弄清楚了。 谢谢!

您的问题的答案有三点。

收集用户输入

gets从stdin返回一行,包括换行符。 如果用户输入B o b enter,gets返回"Bob\\n"

要剥离换行符,请使用String#chomp

"Bob\n".chomp #=> "Bob"

要将"3\\n"类的输入转换为整数3 ,可以使用String#to_i

"3\n".to_i #=> 3

请注意, to_i忽略多余的字符(包括换行符),因此可以避免使用chomp

储存资料

您可能希望将用户数据存储在一个地方。 在像Ruby这样的面向对象的编程语言中,这样的“地方”通常是一个类。 它可以很简单:

class User
  attr_accessor :name, :dinosaurs, :jellyfish
end

attr_accessor创建getter和setter,因此您可以通过以下方式读取和写入用户的属性:

user = User.new
user.name = 'Bob'
user.name #=> "Bob"

由于您不希望只有一个用户,因此您需要另一个位置来存储用户。

Array可以正常工作:

users = []

user = User.new
user.name = 'Bob'
user.dinosaurs = 3

users << user

users
#=> [#<User @name="Bob", @dinosaurs=3>]

添加第二个用户:

user = User.new     # <- same variable name, but new object
user.name = 'Sally'
user.dinosaurs = 2

users << user

users
#=> [#<User @name="Bob", @dinosaurs=3>, #<User @name="Sally", @dinosaurs=2>]

从数组中检索用户(或其属性):

users[0]      #=> #<User @name="Bob">
users[0].name #=> "Bob"
users[1].name #=> "Sally"

用数据计算

Array包括Enumerable mixin中的许多有用方法。

要获得具有最大值的元素,请使用max_by –将每个元素(即用户)传递给一个块,该块必须返回您感兴趣的值(例如dinosaurs )。 然后, max_by返回具有最高dinosaurs值的用户:

users.max_by { |u| u.dinosaurs }
#=> #<User @name="Bob">

您还可以计算值:

users.max_by { |u| u.dinosaurs + u.jellyfish }

但这目前会导致异常,因为我们没有在上面设置jellyfish

现在,您正在尝试通过调用to_f将其转换为浮点数(数字)。 当它来自用户时,它已经是一个字符串了,应该保留一个字符串。

另外,您当前正在循环的每次迭代中覆盖每个变量。 因此,如果Bob填写了此信息,并想添加一个朋友Sally,则Bob的所有信息都会被Sally的信息覆盖。

相反,您需要创建一个哈希或数组,然后从循环中将每个用户一个一个地添加到其中。

continue = "yes"
users = {}

while continue == "yes"
print "Enter your name: "
name = gets.chomp.to_f
print "Enter the number of dinosaurs you have: "
dinosaursCount = gets.chomp.to_f
print "Enter the number of jellyfish you have: "
jellyfishCount = gets.chomp.to_f

users[name] = {dinosaurs: dinosaursCount, jellyfish: jellyfishCount}

print "Another friend? (yes/no)"
continue = gets.chomp
end

现在,如果Bob和Sally都已通过命令行添加,则可以通过执行以下操作获取它们的数据:

users.Bob #{dinosaurs: 10, jellyfish: 10}
users.Bob.dinosaurs #10
users.Bob.jellyfish #10

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM