我是Ruby的初学者,我正在做一个非常简单的“游戏”。
我的问题是,我想测试用户是否按1
然后滚动一些骰子但我不能使用我的方法。请参阅以下代码了解更多详情:
class Player
#i have some methods but it work
end
class Game
tot = 0
def rollDice
tot = 0
puts "You roll the dices"
d1 = rand(1..6)
puts "Dice 1: #{d1}"
d2 = rand(1..6)
puts "Dice 2: #{d2}"
d3 = rand(1..6)
puts "Dice #{d3}"
tot = d1 + d2 + d3
puts "Your score is #{tot}"
return tot
end
#some puts but not important for the coding
puts "Press 1 to play or 2 to leave"
value = gets.chomp
if value == "1"
s1 = rollDice
puts "Why it doesnt work :C #{s1}"
#[i have a player class but its not important for the moment]
#player1 = Joueur.new(j1)
#player2 = Joueur.new(j2)
#player1.score
#player2.score
elsif value== "2"
exit
end
end
但我的方法rollDice有未定义的方法错误 真谢谢你
答案 0 :(得分:2)
您的代码确实有效。但是,你的const qs = require('qs');
const input = {
"reply_message": "This is test",
"decision[0][label]": "Interested",
"decision[0][keyword]": "Interested, call me",
"decision[1][label]": "Not Interested",
"decision[1][keyword]": "not interested, not",
"decision[2][label]": "Call Later",
"decision[2][keyword]": "Interested, call me, later"
};
const output = qs.parse(qs.stringify(input));
console.log(output);
// console:
{ reply_message: 'This is test',
decision:
[ { label: 'Interested', keyword: 'Interested, call me' },
{ label: 'Not Interested', keyword: 'not interested, not' },
{ label: 'Call Later', keyword: 'Interested, call me, later' } ] }
定义是缩进的(并且你有第二次rollDice
调用),所以我猜你实际上是用其他东西(一个类?)来定义的,这在撰写本文时尚未包含在您发布的代码示例中。
顺便说一句,遵循约定/样式指南我建议您将tot=0
重命名为rollDice
。
我成功测试的代码就是这个:
roll_dice
示例会话:
def rollDice
tot = 0
puts "You roll the dices"
d1 = rand(1..6)
puts "Dice 1: #{d1}"
d2 = rand(1..6)
puts "Dice 2: #{d2}"
d3 = rand(1..6)
puts "Dice #{d3}"
tot = d1 + d2 + d3
puts "Your score is #{tot}"
return tot
end☠
puts "Press 1 to play or 2 to leave"
value = gets.chomp
if value == "1"
s1 = rollDice
puts "Why it doesnt work :C #{s1}"
elsif value== "2"
exit
end