显示Wins-Loss-Ties

时间:2014-09-29 17:42:54

标签: java if-statement increment

我正在创建一个Rock,Paper,Scissors游戏,我想让它显示个人的胜利,失败和关系。

当我跑步时,它总是说我输了。

我认为问题在于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

我该如何解决这个问题?

5 个答案:

答案 0 :(得分:0)

您的错误在您的winnerRet方法中。 这段代码:

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

总是将ret设置为0.如果逻辑确实潜入其中一个内部if语句,那么ret变量最后仍会设置为0。 我希望这会有所帮助。

答案 1 :(得分:0)

在你的代码中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;
                }
        }
}

如果条件也适用于其他两种类型的分配。

答案 2 :(得分:0)

虽然这不是直接回答你的问题&#34;错误在哪里&#34;,我认为你的整个解决方案都可以改进。 考虑到这一点,我已经使用了一个枚举的戏剧和一个测试所有游戏规则的简单功能。我认为调试/理解这种方式更快。看看它是否对您有所帮助:

ENUM:

public enum Play{
    ROCK,PAPER,SCISSORS;
}

还有Function,如果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);
    }
}

答案 3 :(得分:0)

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;

    }

}

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

答案 4 :(得分:0)

创建一个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;
      }
    }
  }
}