我正在为学校的班级创建一个石头剪刀布游戏。我得到了一切工作,除了设置一些东西以在回答无效选择时重述问题。我认为 do...while 语句在这里最好,但我意识到我在循环的“do”部分调用了一个变量。所以,因为它从另一个循环调用某些东西,所以它找不到那个变量。另外,在我的 while 语句中,我是否正确列出了可接受的变量?我很确定我只是编造了那部分。
非常感谢你们的帮助!
import java.util.Scanner;
public class RockPaperScissorsTest {
public static void main(String[] args) throws Exception {
Scanner input = new Scanner(System. in );
do {
System.out.println("Player 1, choose 1 for rock, 2 for paper, or 3 for scissors.");
int P1 = input.nextInt();
} while (P != 1; P1 != 2; P1 != 3);
System.out.println("");
System.out.println("");
System.out.println("");
do {
System.out.println("Player 2, choose 1 for rock, 2 for paper, or 3 for scissors.");
int P2 = input.nextInt();
} while (P2 != 1; P2 != 2; P2 != 3);
if (P1 == 1 & P2 == 1)
System.out.println("It's a tie!");
if (P1 == 1 & P2 == 2)
System.out.println("Player 2 wins!");
if (P1 == 1 & P2 == 3)
System.out.println("Player 1 wins!");
if (P1 == 2 & P2 == 1)
System.out.println("Player 1 wins!");
if (P1 == 2 & P2 == 2)
System.out.println("It's a tie!");
if (P1 == 2 & P2 == 3)
System.out.println("Player 2 wins!");
if (P1 == 3 & P2 == 1)
System.out.println("Player 2 wins!");
if (P1 == 3 & P2 == 2)
System.out.println("Player 1 wins");
if (P1 == 3 & P2 == 3)
System.out.println("It's a tie!");
}
}