我不知道这场比赛有什么问题:
print "Welcome to Rock, Paper, Scissors!"
print "
All outcomes are randomly generated and independent from your answer."
puts "
Rock, paper, scissors:"
answer = gets.chomp!
answer.capitalize!
cpu = 3
if cpu == 1
cpu = "Rock"
end
if cpu == 2
cpu = "Paper"
end
if cpu == 3
cpu = "Scissors"
end
print "
Your answer: #{answer}
CPU answer: #{cpu}"
if cpu == answer
print "
Winner: It's a tie"
end
if answer = "Rock" && cpu = "Paper"
print "
Winner: CPU"
end
if answer = "Rock" && cpu = "Scissors"
print "
Winner: Human"
end
它最后输出所有可能的答案,例如:
Your answer: Rock
CPU answer: Scissors
Winner: CPU
Winner: Human
答案 0 :(得分:3)
在代码末尾附近,有几个地方意外使用了赋值运算符(=
)而不是等式检查运算符(==
)。
答案 1 :(得分:0)
这是一种更类似Ruby的方法来解决您的问题。
WINS = { "rock"=>"scissors", "paper"=>"rock", "scissors"=>"paper" }
def outcome(first, second)
return :WIN if WINS[first] == second
return :LOSE if WINS[second] == first
:TIE
end
outcome("rock", "scissors") #=> :WIN
outcome("rock", "paper") #=> :LOSE
outcome("rock", "rock") #=> :TIE
答案 2 :(得分:-2)
亚历山大,
代码末尾有两个错误。
在if
条件中,替换= to ==。
if answer == "Rock" && cpu = "Paper"
print "
Winner: CPU"
end
if answer == "Rock" && cpu = "Scissors"
print "
Winner: Human"
end