我正在开发一款由2人玩的nim游戏。问题是,在1次迭代之后,即使不是他的回合,它仍然会切换到player2。玩家1完全被忽略直到游戏结束。这是我的代码:
int A = 3;
int B = 4;
int C = 5;
Scanner input = new Scanner(System.in);
Scanner x = new Scanner(System.in);
System.out.println("Player 1 name :");
String player1 = input.nextLine();
System.out.println("Player 2 name :");
String player2 = input.nextLine();
System.out.println(" A: " + A + " B: " + B + " C: " + C);
do {
System.out.println(player1 + " choose a pile : ");
String choose = input.nextLine();
if (choose.equals("A")) {
System.out.println("How many to remove from pile A:");
int br = input.nextInt();
if (br == 1) {
A = A - 1;
}
if (br == 2) {
A = A - 2;
} else {
A = A - 3;
}
}
Used same if statements for B & C then repeated all and just switched player.
System.out.println(" A: " + A + " B: " + B + " C: " + C);
System.out.println(player2 + " choose a pile:");
String choose2 = x.nextLine();
if (choose2.equals("A")) {
System.out.println("How many to remove from pile A:");
int br = input.nextInt();
if (br == 1) {
A = A - 1;
}
if (br == 2) {
A = A - 2;
} else {
A = A - 3;
}
}
System.out.println(" A: " + A + " B: " + B + " C: " + C);
} while (A != 0 || B != 0 || C != 0);
答案 0 :(得分:0)
你的错误,因为如果选择1
发生此错误是因为您正在使用alredy使用的相同扫描程序,因此只有第一次迭代才有效。
解决此问题的一种方法是在每次迭代中初始化扫描程序
在你的Do While:
中有这样的东西Scanner sc = new Scanner(System.in);
String choose = sc.nextLine();
第二个:
Scanner sc2 = new Scanner(System.in);
String choose2 = sc2.nextLine();