簡體   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