嘿,我正在尝试找出我在做什么错。我一直在通过netbeans IDE用Java制作剪刀石头布游戏。我知道我已经看到了很多与此有关的问题,但是在我的if语句中,我试图让它在满足条件的情况下打印一些语句。如果玩家1扔了石头,而玩家2扔了任何东西(包括平局),它就会执行此操作,但是除此之外的其他任何事情都不会在运行时打印出语句。我的代码在下面,知道你们喜欢特定的问题和更小的代码部分,但是我觉得需要发布完整的代码以查看我哪里出错了。如果有道理!谢谢你的帮助。
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String personPlay; //Player A -- "R", "P", or "S"
String secondUser; //Player B
Scanner scan = new Scanner(System.in);
System.out.println("Player 1Please enter your name");
String name1;
name1 = scan.next();
System.out.println("Hello " + name1);
System.out.println("Hello Player 2");
System.out.println("Player 2 Please enter your name");
String name2;
name2 = scan.next();
System.out.println("Hello "+name2);
System.out.println(name1 + " enter r for Rock, p for Paper, s for Scissors: "); //Get player's play -- note that this is stored as a string
personPlay = scan.next();
personPlay = personPlay.toLowerCase();
System.out.println(name2 + " enter r for Rock, p for Paper, and s for Scissors");
secondUser = scan.next();
secondUser = secondUser.toLowerCase();
if (personPlay.equals(secondUser)) {
System.out.println("It's a tie!");
} else if (personPlay.equals("r")) {
if (secondUser.equals("s")) {
System.out.println("Rock beats scissors! Victory to "+name1);
} else if (secondUser.equals("p")) {
System.out.println("Paper beats Rock! Victory to "+name2);
} if (personPlay.equals("p")) {
if (secondUser.equals("s")) {
System.out.println("Scissors cut Paper! Victory to "+name2);
} else if (secondUser.equals("r")) {
System.out.println("Paper covers rock! Victory to "+name1);
} if (personPlay.equals("s")) {
if (secondUser.equals("p")) {
System.out.println("Scissors beat paper! Victory to "+name1);
}
} else if (secondUser.equals("r")) {
System.out.println("Rock beats Scissors! Victory to "+name2);
}
}
}
}
}
最佳答案
您检查人员是否扔纸("p"
)或剪刀("s"
)或嵌套在人员扔石头("r"
)的条件内的条件,因此永远不会成立。
检查岩石后,您需要检查此人是否在单独的else if
情况下将纸或剪刀扔了。
} else if (personPlay.equals("r")) {
if (secondUser.equals("s")) {
System.out.println("Rock beats scissors! Victory to "+name1);
} else if (secondUser.equals("p")) {
System.out.println("Paper beats Rock! Victory to "+name2);
}
} else if (personPlay.equals("p")) {
// Test second user is rock and scissors here
} else if (personPlay.equals("s")) {
// Test second user is paper and rock here
}
您还应该添加一些验证检查,以使两个玩家输入的内容仅限于3个合法选择。