提问人:Bheki Fuzwayo 提问时间:3/26/2021 更新时间:3/26/2021 访问量:107
我必须做一个红宝石游戏,猜测一个包含函数但不断出错的随机数
I have to make a ruby game of guessing a random number that incorporates functions but keep getting errors
问:
代码如下: 首先是检查功能
def check ( int, r_int)
if int < r_int
puts "Guess Higher"
tries = tries + 1
elsif int > r_int
puts "Guess Lower"
trie = tries + 1
elsif int == r_int
win = true
puts "You are correct"
puts "You had attempted this "+ tries.to_s + " times to win"
gets.chomp
end
end
那么这是主要功能:
def main
tries = 0
win = false
puts "Lets play a game!"
puts "I am thinking of a number between 1 and 100"
puts "Have a guess"
rnd_int = rand(100)
while not win
guess = gets.chomp.to_i
value = check (guess, rnd_int)
puts value.to_s
end
end
main
这是错误: number_game.rb:42:语法错误,意外的“)”,预期为“=” value = check(猜测,rnd_int) 不知道该怎么做
答:
0赞
Sara Fuerst
3/26/2021
#1
此行发生错误:
value = check (guess, rnd_int)
调用方法时,方法名称和参数列表之间不应有空格。在这种情况下,调用该方法的正确方法是:
value = check(guess, rnd_int)
该错误可能有点令人困惑。它告诉你 Ruby 解释器被 .这告诉您,错误很可能在代码中的此点之前出现。)
有关更多信息,请查看有关方法的一些文档: https://www.w3resource.com/ruby/ruby-methods.php
评论
number_game.rb
check (guess, rnd_int)
check(guess, rnd_int)
def check
(guess, rnd_int)
guess
rnd_int
a > b
a < b
a == b
else
trie = tries + 1
tries += 1