如何在Ruby中使用数组?
我是Ruby的新手。 我正试图用ruby编写一个简单的交互式游戏,但我被卡住了。需要帮助才能继续。 以下是概念:如何在Ruby中使用数组?,ruby,Ruby,我是Ruby的新手。 我正试图用ruby编写一个简单的交互式游戏,但我被卡住了。需要帮助才能继续。 以下是概念: money = [100, 200, 300, 400] beneficiaries = ["John", "Sam", "Mary"] cost1 = [50, 25, 30, 75, 18] cost2 = [120, 150, 200, 250] gift1 = [" crayon ", " Pencil ", "
money = [100, 200, 300, 400]
beneficiaries = ["John", "Sam", "Mary"]
cost1 = [50, 25, 30, 75, 18]
cost2 = [120, 150, 200, 250]
gift1 = [" crayon ", " Pencil ", " Biro "]
gift2 = [" bag ", " shoe ", " Radio "]
cashowned = [50]
a = money.sample
b = cost1.sample
c = cost2.sample
d = gift1.sample
e = gift2.sample
f = cashowned
puts " Hi, what is your name? "
puts
name = gets.chomp
puts
puts " #{name} is a nice name."
puts
puts "#{name} you have #{f} in your bank account."
puts
puts "Roll the dice and let's see how much you just earned."
puts
gets.chomp.to_i
dice1 = a
puts "#{name} you just earned: #{dice1}"
puts
gets.chomp.to_i
Cashowned = dice1 + f
puts "Your account balance is now : #{cashowned}"
puts
gets.chomp.to_i
我被困在这里了。我想重复/循环这个顺序,这样我就可以按成本赠送礼物,并从拥有的现金中扣除。但拥有的现金并没有更新。
一个更简单的方法去做这将是赞赏。
谢谢。Ruby是一种区分大小写的语言 首先将变量声明为:
cashowned = [50]
稍后,您将尝试使用另一个变量名进行更新:
Cashowned = dice1 + f
请注意,“cashowned”
和“cashowned”
是不同的变量
更新:
第7行,您有:
cashowned = [50]
我的问题是,为什么不只使用:
cashowned = 50
?
这样做会导致错误:数组不能强制为Fixnum(TypeError)
您正在尝试将Fixnum与数组求和
参见代码:
money = [100, 200, 300, 400]
beneficiaries = ["John", "Sam", "Mary"]
cost1 = [50, 25, 30, 75, 18]
cost2 = [120, 150, 200, 250]
gift1 = ["crayon", "Pencil", "Biro"]
gift2 = ["bag", "shoe", "Radio"]
cashowned = 50
a = money.sample
b = cost1.sample
c = cost2.sample
d = gift1.sample
e = gift2.sample
f = cashowned
puts "Hi, what is your name?"
name = gets.chomp
puts "#{name} is a nice name."
puts "#{name} you have #{f} in your bank account."
puts "Roll the dice and let's see how much you just earned."
gets.chomp.to_i
dice1 = a
puts "#{name} you just earned: #{dice1}"
gets.chomp.to_i
cashowned = dice1 + f
puts "Your account balance is now : #{cashowned}"
gets.chomp.to_i
并查看测试结果:
如果要更新/访问数组中的值,需要使用索引运算符
cashowned[0] = dice1 + f[0]
这是我做的一个简单的游戏。它并不像我所说的那样完美,但它可以工作,并向您展示我认为您需要的代码类型注意while循环中的while循环和if语句。
#set your variables
b = 200 #initial balance
br = -500 #account limit
dice = [1, 2, 3, 4, 5, 6]
puts "State name?"
n = gets.chomp
puts ""
puts "Greetings #{n}"
puts ""
puts "============================"
puts "Your initial balance is #{b}"
puts "============================"
puts ""
puts "If it reaches or exceeds #{br}"
puts "you'll be declared bankrupt!"
puts ""
while b > br do
#rolling the positive di
puts "Roll good di? (y/n)"
puts ""
a = gets.chomp.downcase
if a == "y"
pd = dice.sample*100
b += pd
puts ""
puts "****you scored #{pd}****"
puts "============================"
puts" new balance: #{b}"
puts "============================"
puts ""
else
puts "============================"
puts "your balance remains at #{b}"
puts "============================"
puts ""
end
#rolling the negative di
puts "Roll evil di? (y/n)"
puts ""
ans = gets.chomp.downcase
if ans == "y"
nd = dice.sample*-100
b += nd
puts ""
puts "****you scored ****#{nd}****"
puts "============================"
puts "new balance: #{b}"
puts "============================"
puts ""
else
puts "============================"
puts "your balance remains at #{b}"
puts "============================"
puts ""
end
#option to repeat
puts ""
puts "Continue playing (y/n)?"
puts ""
ans = gets.chomp.downcase
puts ""
if ans == "n"
puts ""
puts "============================"
puts "final balance is #{b}"
puts "============================"
puts ""
exit
else
b
end
end
puts "You have now reached/exceeded your limit"
puts "of #{br}, so your account has been frozen"
puts ""
运行此代码,查看并修改它以满足您的需要。谢谢。我的主要项目还可以。我只在贴出的问题上有错别字。我仍然不能更新这个变量。它保持它的第一个值。好吧,我放弃。有谁知道如何编写这个简单的游戏吗?你掷骰子,从随机选择奖励的阵列中获得奖励。奖励将添加到拥有的现金中。然后每隔一段时间,你就可以从收入中扣除一笔费用,而不是获得奖励。这个循环一直持续,直到你达到一个预先设定的最大值或破产。请帮忙。