【发布时间】:2015-04-13 21:03:32
【问题描述】:
我必须为我的 ruby 课程创建这个石头、纸、剪刀游戏的分数更新程序有问题。此分配的目的是让用户输入一个选择,计算机也使用“rand”进行随机选择,并且赢得该轮的人将在每轮结束时显示更新的分数。我遇到的问题是计数器仅使用第一个 if 语句进行更新。该程序可以运行到能够随机选择计算机选项并保存用户响应的程度。因此,所有代码都可以使用分数更新器。任何帮助将不胜感激。
human_score = 0.0
comp_score = 0.0
while true
print "Lets play rock,paper,scissors", "\n"
print "Do you choose rock, paper, or scissors?", "\n"
choice = STDIN.gets.chomp
comp = rand(3)
if comp == 1
comp_choice = "rock"
elsif comp == 2
comp_choice = "paper"
else
comp_choice = "scissors"
end
print "Computer picked: ", comp_choice, "\n"
print "You picked: ", choice, "\n"
if human_choice = "rock" && comp_choice = "rock"
comp_score += 0.5
human_score += 0.5
print "The result is a tie", "\n"
elsif human_choice = "rock" && comp_choice = "paper"
comp_score += 1
print "Computer wins", "\n"
elsif human_choice = "rock" && comp_choice = "scissors"
human_score += 1
print "You win", "\n"
elsif human_choice = "paper" && comp_choice = "rock"
human_score += 1
print "You win", "\n"
elsif human_choice = "paper" && comp_choice = "paper"
comp_score += 0.5
human_score += 0.5
print "Its a tie", "\n"
elsif human_choice = "paper" && comp_choice = "scissors"
comp_score += 1
print "Computer wins", "\n"
elsif human_choice = "scissors" && comp_choice = "rock"
comp_score += 1
print "Computer wins"
elsif human_choice = "scissors" && comp_choice = "paper"
human_score += 1
print "You win"
else human_choice = "scissors" && comp_choice = "scissors"
human_score += 0.5
comp_score += 0.5
print "Its a tie"
end
print "The computers score is: ", comp_score, "\n"
print "Your score is: ", human_score, "\n"
end
【问题讨论】:
-
您用于比较选择的
if语句都使用=,它已成功分配给这些变量;将这些更改为==。
标签: ruby