【问题标题】:Displaying Wins-Loss-Ties显示输赢关系
【发布时间】:2014-09-29 17:42:53
【问题描述】:

我正在创建一个石头剪刀布游戏,我想让它显示个人的赢、输和平局。

当我运行它时,它总是说我输了。

我认为问题在于 if-then 语句会增加正确的值。

if(ret == 1)//starts the increases of wins and losses
           {
               if(ret != 0)
               {
                   if (ret != 2)
                   {
                    w += 1;
                   }
               }
           }

           if(ret == 0)
           {
               if(ret != 1)
               {
                   if(ret != 2)
                   {
                       l += 1;
                   }
               }
           }

           if(ret == 2)
           {
               if(ret != 1)
               {
                   if(ret != 0)
                   {
                       t += 1;
                   }
               }
           }


或者在这种方法中,确定该人是赢还是输。
public static int winnerRet(char user, char compGuess)// method to determine winner
   {
        int ret = 3;
        if(user == 'R')
        {
            if(compGuess != 'P')
            {
                if(compGuess != 'R')
                {
                    ret = 1;
                }
                ret = 2;
            }
            ret = 0;
        }
        if(user == 'S')
        {
            if(compGuess != 'R')
            {
                if(compGuess != 'S')
                {
                    ret = 1;
                }
                ret = 2;
            }
            ret = 0;
        }
        if(user == 'P')
        {
            if(compGuess != 'S')
            {
                if(compGuess != 'P')
                {
                ret = 1;
                }
                ret = 2;
            }
            ret = 0;
        }
        return ret;
   }//end winnerRet

我认为最后可能是生成计算机选择的方法。

   public static char compChoice()//starts method to generate computure choice
   {
        Random random = new Random();
        int compNum;
        char compGuess = '\0';
        compNum = 1 + random.nextInt(3);
        if (compNum == 1)
        {
            compGuess = 'R';
        }
        if(compNum == 2)
        {   
            compGuess = 'S';
        }
        if(compNum == 3)
        {
            compGuess = 'P';
        }
        return compGuess;
   }//end method compChoice

我该如何解决这个问题?

【问题讨论】:

  • 运行程序有没有报错?
  • 您可能想尝试将 if/else 语句与逻辑运算符(&& 和 ||)结合起来,以使其可读
  • 很多 if 语句都是多余的。如果它等于某个东西,它肯定不能等于其他两个。
  • 您的代码非常不透明,您可能应该在多个地方使用枚举。
  • 让我们从if(ret == 1) { if(ret != 0) { if (ret != 2){ w += 1; } } } 开始,你的逻辑是什么? 1 已经不是 0 或 2....

标签: java if-statement increment


【解决方案1】:

虽然这不是对您“错误在哪里”的问题的直接回答,但我认为您的整个解决方案可以改进。 考虑到这一点,我使用了游戏的枚举和一个测试所有游戏规则的简单函数。我认为以这种方式调试/理解会更快。看看对你有没有帮助:

枚举:

public enum Play{
    ROCK,PAPER,SCISSORS;
}

还有一个函数,如果 player1 输了则返回 false,如果 player1 获胜则返回 true:

public boolean play(Play player1, Play player2) throws UnsuportedPlayException{
    // rock wins scissors
    if(player1 == Play.ROCK && player2 == Play.SCISSORS)
        return true;
    if(player2 == Play.ROCK && player1 == Play.SCISSORS)
        return false;

    //rock loses to paper
    if(player2 == Play.ROCK && player1 == Play.PAPER)
        return true;
    if(player1 == Play.ROCK && player2 == Play.PAPER)
        return false;

    //paper loses to scissors
    if(player1 == Play.PAPER && player2 == Play.SCISSORS)
        return false;
    if(player2 == Play.PAPER && player1 == Play.SCISSORS)
        return true;
    throw new UnsuportedPlayException("That play is not yet available.");
}

这个例外是我创建的,只是为了处理空值,或者如果您决定添加游戏而忘记添加规则(您可能想添加第四个可能的游戏,只是为了好玩)。

异常类:

public class UnsuportedPlayException extends Exception {

    private static final long serialVersionUID = 1L;

    public UnsuportedPlayException() {
        super();
    }

    public UnsuportedPlayException(String message, Throwable cause) {
        super(message, cause);
    }

    public UnsuportedPlayException(String message) {
        super(message);
    }

    public UnsuportedPlayException(Throwable cause) {
        super(cause);
    }
}

【讨论】:

  • 嘿,另一个简单的问题,当我尝试实施您的建议时,它给了我错误“找不到符号 - 类 UnsuportedPlayException”。你知道为什么会这样吗?
  • 这是一个异常类。您必须使用我在“异常类”之后添加的代码创建类。如果您不熟悉 Java 中的异常,您应该阅读一些有关异常的内容。这是一个开始:docs.oracle.com/javase/tutorial/essential/exceptions
  • 对不起,我没有看到最后一点。我的错。
  • 没问题,我后来加了。如果它解决了您的问题,请不要忘记投票/接受作为答案:)
【解决方案2】:

您的错误出现在您的 WinnerRet 方法中。 这段代码:

if(user == 'R')
        {
            if(compGuess != 'P')
            {
                if(compGuess != 'R')
                {
                    ret = 1;
                }
                ret = 2;
            }
            ret = 0;
        }

将始终将 ret 设置为 0。如果逻辑确实深入到内部 if 语句之一,则 ret 变量最后仍会设置为 0。 我希望这会有所帮助。

【讨论】:

    【解决方案3】:

    在您的代码中,ret=0 是 if 条件的最后一条语句,因此它将始终将 ret 值分配给 0

    而不是

    if(user == 'R'){
                if(compGuess != 'P')
                {
                    if(compGuess != 'R')
                    {
                        ret = 1;
                    }
                    ret = 2;
                }
                ret = 0;
    }
    

    使用下面的代码

    if (user == 'R') {
                ret = 0;
                if (compGuess != 'P') {
                    ret = 2;
                    if (compGuess != 'R') {
                        ret = 1;
                    }
            }
    }
    

    对其他两个 if 条件也使用这种类型的赋值。

    【讨论】:

      【解决方案4】:
      import java.util.Random;
      
      public class RockPaperScissor {
      
          static String ret;
      
          public static void main(String args[]) {
      
              String computer = compChoice();
              winnerRet(args[0], computer);
      
          }
      
          public static void winnerRet(String user, String compGuess) {
      
              if (user.equals("R")) {
                  if (!compGuess.equals("P")) {
                      if (!compGuess.equals("R")) {
                          ret = "Win";
                      } else {
                          ret = "Draw";
                      }
                  } else {
                      ret = "Lose";
                  }
              } else if (user.equals("S")) {
                  if (!compGuess.equals("R")) {
                      if (!compGuess.equals("S")) {
                          ret = "Win";
                      } else {
                          ret = "Draw";
                      }
                  } else {
                      ret = "Lose";
                  }
              } else if (user.equals("P")) {
                  if (!compGuess.equals("S")) {
                      if (!compGuess.equals("P")) {
                          ret = "Win";
                      } else {
                          ret = "Draw";
                      }
                  } else {
                      ret = "Lose";
                  }
              }
      
              System.out.println(compGuess);
              System.out.println(ret);
      
          }
      
          public static String compChoice() {
      
              String compGuess;
      
              Random random = new Random();
              int compNum = random.nextInt(3) + 1;
      
              if (compNum == 1) {
                  compGuess = "R";
              } else if (compNum == 2) {
                  compGuess = "S";
              } else {
                  compGuess = "P";
              }
      
              return compGuess;
      
          }
      
      }
      

      嗯,我很快就根据您的代码构建了一个没有计数部分的石头剪刀布游戏。它似乎正在工作。好吧,我想您可以查看我的代码并根据它编辑您的代码。我认为问题在于您将字符与== 进行比较。我用的是字符串,所以你可以互换它。

      【讨论】:

        【解决方案5】:

        创建一个 Move 枚举并让它为您完成工作。它比所有 if 语句更具可读性。 另外,您需要 initMoves 的原因是您不能在初始化之前引用枚举(但是它们相互引用)。

        import java.util.Random;
        
        public class RockPaperScissors {
        
          public static void main(String[] args) {
            Move.initMoves();
            Move computerMove = Move.randomMove();
            Move.ROCK.printWin(computerMove);
            Move.PAPER.printWin(computerMove);
            Move.SCISSORS.printWin(computerMove);
          }
        
          private static enum Move {
        
            ROCK,
            PAPER,
            SCISSORS;
        
            private Move beats;
        
            private Move() {}
        
            private static void initMoves() {
              ROCK.beats = SCISSORS;
              PAPER.beats = ROCK;
              SCISSORS.beats = PAPER;
            }
        
            public void printWin(Move computer) {
              if (this == computer) {
                System.out.println("It was a tie.");
              } else if (this.beats == computer) {
                System.out.println("Player wins.");
              } else {
                System.out.println("Computer wins.");
              }
            }
        
            public static Move randomMove() {
              int move = new Random().nextInt(3);
              switch(move) {
              case 0:
                return ROCK;
              case 1:
                return PAPER;
              default:
                return SCISSORS;
              }
            }
          }
        }
        

        【讨论】:

          猜你喜欢
          • 2021-04-26
          • 1970-01-01
          • 2012-04-04
          • 2020-07-25
          • 1970-01-01
          • 1970-01-01
          • 2022-01-10
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多