Ruby-循环使用gets.chomp进行计算 [英] Ruby - calculations with gets.chomp in a loop

查看:170
本文介绍了Ruby-循环使用gets.chomp进行计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm very new to Ruby. I've been trying to find a way to do calculations with user inputs. There are two things that I wanted to do:

  1. 输出哪个朋友的恐龙或水母最多.
  2. 输出哪个朋友拥有最多的恐龙和水母.

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

I only have this loop requesting for user inputs so far:

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

感谢您的帮助.非常感谢!

Any help is appreciated. Thank you so much!

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

UPDATES: Thank you so much for you all's help. I've realized that I was not specific enough. I'll try to clarify that here.

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

The output that I want looks something like this (things in ~ ~ are user inputs):

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输出我想要的最后三行.你们可以为大家阐明一下吗?

So far, the codes that I wrote only gets me to "Another friend? (yes/no)" but I'm not sure how to ask Ruby to output the last three lines that I want. Can you folks shed some light on this, please?

非常感谢!

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

MORE UPDATES: Thanks for all of your help! Got it figured out with Hash and Array. Thank you!

推荐答案

您的问题的答案有三点.

The answer to your question is three-fold.

gets 返回一个来自stdin的行,包括换行符.如果用户输入 B o b enter ,则gets返回"Bob\n".

要删除换行符,请使用 String#chomp :

To strip the newline you use String#chomp:

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

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

To convert an input like "3\n" to an integer 3, you can use String#to_i:

"3\n".to_i #=> 3

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

Note that to_i ignores extraneous characters (including newline), so you can avoid chomp.

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

You probably want to store a user's data in one place. In an object-oriented programming language like Ruby, such "place" is often a class. It can be as simple as:

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

attr_accessor 创建吸气剂和设置器,因此您可以通过以下方式读取和写入用户的属性:

attr_accessor creates getters and setters, so you can read and write a user's attributes via:

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

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

Since you don't want just one user, you need another place to store the users.

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>]

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

Retrieving the users (or their attributes) from the array:

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

计算数据

Array包括 Enumerable mixin.

Calculating with data

Array includes many useful methods from the Enumerable mixin.

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

To get an element with a maximum value there's max_by – it passes each element (i.e. user) to a block and the block has to return the value you are interested in (e.g. dinosaurs). max_by then returns the user with the highest dinosaurs value:

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

您还可以计算值:

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

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

But that would currently result in an exception, because we did not set jellyfish above.

这篇关于Ruby-循环使用gets.chomp进行计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆