0

学习java很明显。我能够使游戏正常运行。但我需要这样做,以便如果用户放置 R/P/S 以外的内容,则默认为 Rock。我不需要循环。如果我放石头,游戏就完美了。如果我放了 RPS 以外的任何东西,它也可以完美运行,并且默认为摇滚。但是,如果我做纸或剪刀,它会给我纸和剪刀的答案,并且默认为摇滚答案。关于为什么这不起作用的任何提示?

另外,我的方向错了吗?我可以这样做更好吗?这感觉真的......充其量是不雅的。

    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!");
                    } 
            }
    }

}

4

3 回答 3

3

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

if (paper) {
    process paper
}

if (scissors) {
    process scissors
}

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

请注意(因为情况是互斥的)如果paperorscissors为真,rock则为假并且else将执行。只需将结构更改为:

if (paper) {
    process paper
} else if (scissors) {
    process scissors
} else if (rock) {
    process rock
} else {
    process default rock
}
于 2013-02-08T05:22:04.670 回答
3

你的代码需要有else if'S'分支和'R'分支,否则逻辑如下(对于'S'输入):

if (P) 做某事 // 不,所以继续
if (S) 做某事 // 是的,'S' 的东西
也做.

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

于 2013-02-08T05:22:34.513 回答
1

您询问了“优雅”的解决方案,一种方法是使用获胜矩阵,换句话说,如下所示:

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()];
   }
}

然后您可以通过简单地调用枚举的 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
   }
}
于 2013-02-08T05:54:45.723 回答