4
import java.util.Scanner;
public class RockPaperScissors
 {
public static void main (String[] args)
{
    Scanner scan = new Scanner (System.in);
    System.out.println("Player 1: Choose rock, paper or scissors: ");
    String Player1 = scan.next().toLowerCase();
    System.out.println("Player 2: Choose rock, paper or scissors: ");
    String Player2 = scan.next().toLowerCase();
    System.out.println ("Player 1 chose " + Player1);
    System.out.println ("Player 2 chose " + Player2);

    if (Player1.!equals("rock" + "paper" + "scissors")
    {
        System.out.println ("Please insert either Rock Paper or Scissors!);
    }
    if (Player1.equals(Player2))
    {
        System.out.println ("Its a tie!");
    }
    if ((Player1.equals("rock")) && (Player2.equals("paper")))
    {
        System.out.println ("Player 2 wins!");
    }
    else if ((Player1.equals("paper")) && (Player2.equals("rock")))
    {
        System.out.println ("Player 1 wins!");
    }
    if ((Player1.equals("scissors")) && (Player2.equals("paper")))
    {
        System.out.println ("Player 1 wins!");
    }
    else if ((Player1.equals("paper")) && (Player2.equals("scissors")))
    {
        System.out.println ("Player 2 wins!");
    }
    if ((Player1.equals("rock")) && (Player2.equals("scissors")))
    {
        System.out.println ("Player 1 wins!");
    }
    else if ((Player1.equals("scissors")) && (Player2.equals("rock")))
    {
        System.out.println ("Player 2 wins!");
    }


}

}

我制作了一个简单的剪刀石头布游戏,但是我想实现验证,以便用户在输入除石头剪刀布以外的任何内容时会收到通知。我试过了,.!equals但是我收到一个错误,说是<identifier>预期的。

谢谢!

4

5 回答 5

10

错误的语法:

Player1.!equals

改成:

!Player1.equals
于 2012-10-29T12:33:58.623 回答
4

正确的语法是

if (!Player1.equals("rock" + "paper" + "scissors"))

无论如何,这不会满足您的目的,它应该是:

if (!Player1.equals("rock") 
    && !Player1.equals("paper") 
    && !Player1.equals("scissors"))

或同等的

if (!(Player1.equals("rock")
    || Player1.equals("paper") 
    || Player1.equals("scissors")))

运算符连接字符串,因此+您将Player1"rockpaperscissors"


作为旁注,您应该:

  • 尊重 Java 约定,并以小写字母(player1而不是Player1)开头你的 var 名称
  • 将您的固定字符串提取为常量(private static final String PAPER = "paper";作为类属性)
  • 尽可能调用.equals()常量,以避免NullPointerException(使用PAPER.equals(player1)

总而言之,您的代码如下所示:

if (!ROCK.equals(player1) 
    && !PAPER.equals(player1) 
    && !SCISSORS.equals(player1))
于 2012-10-29T12:33:52.013 回答
1

试试这样:

  if (!Player1.equals("rock" + "paper" + "scissors")
{
    System.out.println ("Please insert either Rock Paper or Scissors!);
}
于 2012-10-29T12:33:50.567 回答
1

你的代码甚至不会编译你需要这样的东西:

if (!Player1.equals("rock") && !Player1.equals("paper") && !Player1.equals("scissors")) {
   System.out.println ("Please insert either Rock Paper or Scissors!);
}
于 2012-10-29T12:35:13.507 回答
1

除了语法之外,测试在逻辑上也是错误的。这:

if (!Player1.equals("rock" + "paper" + "scissors")) {

意思和这个一样:

if (!Player1.equals("rockpaperscissors")) {

你真正需要的是这样的:

if (!(Player1.equals("rock") || 
      Player1.equals("paper") || 
      Player1.equals("scissors"))) {

......其余的逻辑也有点坏了。


最后,我应该指出,Player1应该Player2player1player2分别。变量名不应该以大写字母开头。这是一个很大的文体禁忌。

于 2012-10-29T12:36:51.480 回答