【问题标题】:Ruby Rock, Paper, Scissors红宝石岩石,纸,剪刀
【发布时间】: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


【解决方案1】:

您正在重新分配变量的值,而不是检查是否相等。

您的版本:

if human_choice = "rock" && comp_choice = "rock"

这会将human_choice 的值设置为“rock”,与comp_choice 相同。永远都是真的。

正确版本:

if human_choice == "rock" && comp_choice == "rock"

【讨论】:

    【解决方案2】:

    另外,您将从键盘读取的值分配给choice

    choice = STDIN.gets.chomp
    

    您正在使用human_choice 来验证案例。

    【讨论】:

      【解决方案3】:

      思考这个:

      human_score = 0.0
      comp_score = 0.0
      
      while true
      
        puts "Lets play rock, paper, scissors"
        puts "Do you choose rock, paper, or scissors?"
        human_choice = STDIN.gets.chomp
      
        comp_choice = %w[scissors rock paper][rand(3)]
      
        puts "Computer picked: #{ comp_choice }"
        puts "You picked: #{ human_choice }"
      
        msg = case [human_choice, comp_choice]
        when ['rock', 'rock'], ['paper', 'paper'], ['scissors', 'scissors']
          comp_score += 0.5
          human_score += 0.5
          'The result is a tie'
        when ['rock', 'scissors'], ['paper', 'rock'], ['scissors', 'paper']
          human_score += 1
          'You win'
        when ['paper', 'scissors'], ['scissors', 'rock'], ['rock', 'paper']
          comp_score += 1
          'Computer wins'
        end
      
        puts msg
        puts "The computer's score is: #{ comp_score }"
        puts "Your score is: #{ human_score }"
      
      end
      

      它是使用惯用的 Ruby 编写的。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-06-20
        • 2013-12-09
        • 1970-01-01
        • 2019-08-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多