Java Rock / Paper / Scissors,做错了吗?

时间:2013-02-08 05:17:36

标签: java

明显学习java。我能够让游戏运作良好。但我需要做到这一点,如果用户放置R / P / S以外的东西,那么它默认为Rock。我不需要循环。如果我放摇滚,游戏的效果会很完美。如果我放置除RPS以外的任何东西,它也可以完美运行并且默认为摇滚。但是,如果我做Paper或Scissors,它会给我纸和剪刀的答案,并且默认为摇滚答案。为什么这不起作用的任何提示?

另外,我的方向错了吗?我可以这样做更好吗?这感觉真的......最多只是不优雅。

    public static void main(String[] args) {
            // TODO Auto-generated method stub
    Scanner in = new Scanner(System.in);
    String userChoice="", userInput, compChoice="";
    int ranInt = (int)(Math.random()*3);
    if (ranInt == 0){
            compChoice = "Rock";
    } else if (ranInt == 1){
            compChoice = "Paper";
    }else if (ranInt == 2){
            compChoice = "Scissors";
    }
    System.out.println("Please select one of [R/P/S]: ");
            userInput = in.next();

    if(userInput.equalsIgnoreCase("p")){
            userChoice = "Paper ";
            if(compChoice.equalsIgnoreCase("Rock")){
                    System.out.println("You chose: "+userChoice);
                    System.out.println("I chose: "+compChoice);
                    System.out.println(userChoice + "beats "+compChoice +"- you win!");
            }else if (compChoice.equalsIgnoreCase("Paper")){
                    System.out.println("You chose: "+userChoice);
                    System.out.println("I chose: "+compChoice);
                    System.out.println("A tie!");
            }else if(compChoice.equalsIgnoreCase("Scissors")){
                    System.out.println("You chose: "+userChoice);
                    System.out.println("I chose: "+compChoice);
                    System.out.println(compChoice + " beats " + userChoice + " - i win!");
                    }
            }

    if(userInput.equalsIgnoreCase("S")){
            userChoice = "Scissors ";
            if(compChoice.equalsIgnoreCase("Paper")){
                    System.out.println("You chose: "+userChoice);
                    System.out.println("I choce: "+compChoice);
                    System.out.println(userChoice + "beats "+compChoice +"- you win!");
            }else if(compChoice.equalsIgnoreCase("Scissors")){
                    System.out.println("You chose: "+userChoice);
                    System.out.println("I chose: "+compChoice);
                    System.out.println("A tie!");
            }else if(compChoice.equalsIgnoreCase("Rock")){
                    System.out.println("You chose: "+userChoice);
                    System.out.println("I chose: "+compChoice);
                    System.out.println(compChoice + " beats " + userChoice + " - i win!");
                    }
            }              
    if(userInput.equalsIgnoreCase("R")){
            userChoice = "Rock ";
            if(compChoice.equalsIgnoreCase("Rock")){
                    System.out.println("You chose: "+userChoice);
                    System.out.println("I chose: "+compChoice);
                    System.out.println("A Tie!");
            }else if(compChoice.equalsIgnoreCase("Paper")){
                    System.out.println("You chose: "+userChoice);
                    System.out.println("I chose: "+compChoice);
                    System.out.println(compChoice + " beats " + userChoice + " - you lose!");
            }else if(compChoice.equalsIgnoreCase("Scissors")){
                    System.out.println("You chose: "+userChoice);
                    System.out.println("I chose: "+compChoice);
                    System.out.println(userChoice + " beats " + compChoice + " - you win!");
                    }
            }
    else {
            userChoice = "";
            System.out.println("Invalid selection, defaulting to rock.");
            if(compChoice.equalsIgnoreCase("Rock")){
                    System.out.println("You chose: Rock");
                    System.out.println("I chose: "+compChoice);
                    System.out.println("A Tie!");
            }else if (compChoice.equalsIgnoreCase("Paper")){
                    System.out.println("You chose: Rock");
                    System.out.println("I chose: "+compChoice);
                    System.out.println(compChoice + " beats " + "Rock - you lose!");
            } else if(compChoice.equalsIgnoreCase("Scissors")){
                    System.out.println("You chose: Rock");
                    System.out.println("I chose: "+compChoice);
                    System.out.println("Rock beats " + compChoice + " - you win!");
                    } 
            }
    }

}

3 个答案:

答案 0 :(得分:3)

在伪代码中,代码的结构是:

if (paper) {
    process paper
}

if (scissors) {
    process scissors
}

if (rock) {
    process rock
} else {
    process default rock
}

请注意(如果案例是互斥的),如果paperscissors为真,则rock为false,else将执行。只需将结构更改为:

if (paper) {
    process paper
} else if (scissors) {
    process scissors
} else if (rock) {
    process rock
} else {
    process default rock
}

答案 1 :(得分:3)

您的代码需要{S}分支和'R'分支else if,否则逻辑进行如下(对于'S'输入):

如果(P)做某事//不,那么继续执行 如果(S)做某事//是的,'S'的东西也是如此 如果(R)做其他事情做不同的事情//不是'R',所以做一些不同的事情。

有关在代码中添加“优雅”的想法,请查看Rock, Paper, Scissors

的Rosetta代码页

答案 2 :(得分:1)

你问过“优雅”的解决方案,一种方法是使用win矩阵,换句话说就是这样:

public enum RPS {
   ROCK, PAPER, SCISSORS;

   public int test(RPS otherRps) {
      int[][] winMatrix = { 
            {  0, -1,  1 }, 
            {  1,  0, -1 }, 
            { -1,  1,  0 } };
      return winMatrix[ordinal()][otherRps.ordinal()];
   }
}

然后你可以通过简单地调用enum的test(...)方法来测试结果。例如:

import java.util.Random;
import java.util.Scanner;

public class RockPaperScissors {
   private static Random random = new Random();
   private static Scanner in = new Scanner(System.in);

   public static void main(String[] args) {
      RPS compChoice = RPS.values()[random.nextInt(RPS.values().length)];
      RPS userChoice = getUserChoice();

      String result = "";
      switch (userChoice.test(compChoice)) {
      case 1:
         result = "you win!";
         break;

      case -1:
         result = "you lose!";
         break;

      case 0:
         result = "it's a tie";

      default:
         break;
      }
      System.out.printf("You chose %s, the computer chose %s, %s%n",
            userChoice, compChoice, result);
   }

   private static RPS getUserChoice() {
      System.out.print("Please select one of [R/P/S]: ");
      String userChoiceStr = in.nextLine().trim();
      if (userChoiceStr.isEmpty()) {
         return RPS.ROCK;
      }
      for (RPS rps : RPS.values()) {
         if (rps.toString().startsWith(userChoiceStr.toUpperCase())) {
            return rps;
         }
      }

      return RPS.ROCK; // default
   }
}