1

希望这是一个快速的问题。我正在制作一个简单的石头剪刀布游戏。我做一个随机的计算机选择很好,让用户的选择很好。但是当我试图找出谁获胜时,它会在我的 else if 块中打印最后一个 else if 用于无效输入。

当做出正确的选择时,它会打印“输入一个有效的选择”。

import java.util.Random;
import javax.swing.JOptionPane;

public class JavaApplication4 {

    public static void main(String[] args) 
    {
        Random ranNums = new Random();

        int comp = ranNums.nextInt(3);
        String comp2;
        String winner;

        String user = JOptionPane.showInputDialog
                (null, "Enter rock, paper, or scissors");
        user.toLowerCase();

        if(comp == 0)
            comp2 = "rock";
        else if(comp == 1)
            comp2 = "paper";
        else 
            comp2 = "scissors";

        //Computer wins
        if(comp2 == "rock" && user == "scissors")
            winner = "The computer wins";

        else if(comp2 == "paper" && user == "rock")
            winner = "The computer wins";    

        else if(comp2 == "scissors" && user == "paper")
            winner = "The computer wins";

        //Tie game
        else if(comp2 == "rock" && user == "rock")
            winner = "It's a tie";

        else if(comp2 == "paper" && user == "paper")
            winner = "It's a tie";

        else if(comp2 == "scissors" && user == "scissors")
            winner = "It's a tie";

        //User wins
        else if(comp2 == "scissors" && user == "rock")
            winner = "You win!";

        else if(comp2 == "rock" && user == "paper")
            winner = "You win!";

        else if(comp2 == "paper" && user == "scissors")
            winner = "You win!";
        else
            winner = "Enter a valid choice";

        JOptionPane.showMessageDialog(null, "You picked " + user + "\n" +
            "The computer picked " + comp2 + "\n" +
                winner);



    }
}
4

4 回答 4

4

你不能用

comp == "paper" 

比较java中的字符串。你用

comp.equals("paper")

或者如果大小写无关紧要

comp.equalsIgnoreCase("paper")
于 2012-04-06T01:37:27.897 回答
3

不要使用==. 请改用 equals 或 equalsIgnoreCase(...) 方法。了解 == 检查两个对象是否相同,这不是您感兴趣的。另一方面,这些方法检查两个字符串是否具有相同顺序的相同字符,这在这里很重要。所以而不是

if (fu == "bar") {
  // do something
}

做,

if ("bar".equals(fu)) {
  // do something
}

或者,

if ("bar".equalsIgnoreCase(fu)) {
  // do something
}

简化代码的一种方法是创建 RockPaperScissors 枚举并为其提供比较方法。就像是:

enum RockPaperScissors {
   ROCK("Rock"), PAPER("Paper"), SCISSORS("Scissors");

   private String text;
   private static int[][] winMatrix = {{0, -1, 1}, {1, 0, -1}, {-1, 1, 0}};

   private RockPaperScissors(String text) {
      this.text = text;
   }

   @Override
   public String toString() {
      return text;
   }

   // can't use compareTo since it is a final method for enums
   public int compareVs(RockPaperScissors other) {
      int thisOrdinal = ordinal();
      int otherOrdinal = other.ordinal();
      return winMatrix[thisOrdinal][otherOrdinal];
   }
}

然后比较一个枚举与另一个枚举,只需调用它的 compareVs(...) 方法传入另一个枚举。

所以你巨大的 if/else 块将减少到:

// assuming that user and comp are RockPaperScissors variables
int result = user.compareVs(comp);
if (result == 1) {
   System.out.println("You've won!");
} else if (result == 0) {
   System.out.println("It's a tie!");
} if (result == -1) {
   System.out.println("You've lost!");
}
于 2012-04-06T01:36:38.183 回答
2

如果你像我一样喜欢原始代码:

import java.util.Random; 
import javax.swing.JOptionPane;

public class JavaApplication4 {

public static void main(String[] args) 
{
    Random ranNums = new Random();

    int comp = ranNums.nextInt(3);
    String comp2;
    String winner;

    String user = JOptionPane.showInputDialog
            (null, "Enter rock, paper, or scissors");
    user.toLowerCase();

    if(comp == 0)
        comp2 = "rock";
    else if(comp == 1)
        comp2 = "paper";
    else 
        comp2 = "scissors";

    //Computer wins
    if(comp2.equals("rock") && user.equals( "scissors"))
        winner = "The computer wins";

    else if(comp2.equals("paper") && user.equals( "rock"))
        winner = "The computer wins";    

    else if(comp2.equals("scissors") && user.equals( "paper"))
        winner = "The computer wins";

    //Tie game
    else if(comp2.equals("rock") && user.equals( "rock"))
        winner = "It's a tie";

    else if(comp2.equals("paper") && user.equals( "paper"))
        winner = "It's a tie";

    else if(comp2.equals("scissors") && user.equals( "scissors"))
        winner = "It's a tie";

    //User wins
    else if(comp2.equals("scissors") && user.equals( "rock"))
        winner = "You win!";

    else if(comp2.equals("rock") && user.equals( "paper"))
        winner = "You win!";

    else if(comp2.equals("paper") && user.equals( "scissors"))
        winner = "You win!";
    else
        winner = "Enter a valid choice";

    JOptionPane.showMessageDialog(null, "You picked " + user + "\n" +
        "The computer picked " + comp2 + "\n" +
            winner);
}
}

希望有帮助!

于 2012-04-06T01:43:41.593 回答
1

你不能像这样比较字符串:if (comp2 == "rock")

你需要写:if ("rock".equals(comp2))

于 2012-04-06T01:36:43.053 回答