带 if 语句的 While 循环不会跟随连续的 if 语句
While loop w/ if statement won't follow sucesive if statements
我正在做一个在 Internet 上找到的练习来模拟游戏 "nim"。游戏的想法是让其他玩家拿起最后一件物品。我遇到的问题是在玩家 1 选择要从哪个堆中挑选以及从该堆中挑选多少物品之后。最初这很好,但是当轮到玩家 2 时,总是选择他之前的玩家堆,而不管他们选择的堆,它从与玩家 1 相同的堆中扣除他们指定的数量。这只发生在我添加了较低的 - if 语句中的 case 选项。有人能解释一下为什么它总是选择前面的玩家堆吗?
while(pileA > 0 || pileB > 0 || pileC > 0){
System.out.println(playerOne+", which pile would you like to pick from?");
String choice = input.next();
System.out.println("How many cards would you like to take from pile "+choice+"?");
int amount = input.nextInt();
if(choice.equals("A") || choice.equals("a")){
pileA = pileA - amount;
System.out.println("A: "+pileA+" B: "+pileB+" C: "+pileC);
}
else if(choice.equals("B") || choice.equals("b")){
pileB = pileB - amount;
System.out.println("A: "+pileA+" B: "+pileB+" C: "+pileC);
}
else if(choice.equals("C") || choice.equals("c")){
pileC = pileC - amount;
System.out.println("A: "+pileA+" B: "+pileB+" C: "+pileC);
}
else{
System.out.println("ERROR");
}
System.out.println(playerTwo+", which pile would you like to pick from?");
String choiceTwo = input.next();
System.out.println("How many cards would you like to take from pile "+choiceTwo+"?");
amount = input.nextInt();
if(choiceTwo.equals("A") || choice.equals("a")){
pileA = pileA - amount;
System.out.println("A: "+pileA+" B: "+pileB+" C: "+pileC);
}
else if(choiceTwo.equals("B") || choice.equals("b")){
pileB = pileB - amount;
System.out.println("A: "+pileA+" B: "+pileB+" C: "+pileC);
}
else if(choiceTwo.equals("C") || choice.equals("c")){
pileC = pileC - amount;
System.out.println("A: "+pileA+" B: "+pileB+" C: "+pileC);
}
else{
System.out.println("ERROR");
}
}
当您为播放器添加两个选项时,您似乎复制了 if 语句并忘记将小写选项的 choice.equals
转换为 choiceTwo.equals
if(choiceTwo.equals("A") || choiceTwo.equals("a")){
pileA = pileA - amount;
System.out.println("A: "+pileA+" B: "+pileB+" C: "+pileC);
}
else if(choiceTwo.equals("B") || choiceTwo.equals("b")){
pileB = pileB - amount;
System.out.println("A: "+pileA+" B: "+pileB+" C: "+pileC);
}
else if(choiceTwo.equals("C") || choiceTwo.equals("c")){
pileC = pileC - amount;
System.out.println("A: "+pileA+" B: "+pileB+" C: "+pileC);
}
您正在检查 player1 的输入,而您应该检查 player2 的小写输入:
if(choiceTwo.equals("A") || choice.equals("a")){ // should be || choiceTwo.equals("a")
pileA = pileA - amount;
System.out.println("A: "+pileA+" B: "+pileB+" C: "+pileC);
}
else if(choiceTwo.equals("B") || choice.equals("b")){ // should be || choiceTwo.equals("b")
pileB = pileB - amount;
System.out.println("A: "+pileA+" B: "+pileB+" C: "+pileC);
}
else if(choiceTwo.equals("C") || choice.equals("c")){ // should be || choiceTwo.equals("c")
pileC = pileC - amount;
System.out.println("A: "+pileA+" B: "+pileB+" C: "+pileC);
}
错误在
if(choiceTwo.equals("A") || choice.equals("a")){
pileA = pileA - amount;
你应该使用 choiceTwo 而不是第二个玩家的选择。
我正在做一个在 Internet 上找到的练习来模拟游戏 "nim"。游戏的想法是让其他玩家拿起最后一件物品。我遇到的问题是在玩家 1 选择要从哪个堆中挑选以及从该堆中挑选多少物品之后。最初这很好,但是当轮到玩家 2 时,总是选择他之前的玩家堆,而不管他们选择的堆,它从与玩家 1 相同的堆中扣除他们指定的数量。这只发生在我添加了较低的 - if 语句中的 case 选项。有人能解释一下为什么它总是选择前面的玩家堆吗?
while(pileA > 0 || pileB > 0 || pileC > 0){
System.out.println(playerOne+", which pile would you like to pick from?");
String choice = input.next();
System.out.println("How many cards would you like to take from pile "+choice+"?");
int amount = input.nextInt();
if(choice.equals("A") || choice.equals("a")){
pileA = pileA - amount;
System.out.println("A: "+pileA+" B: "+pileB+" C: "+pileC);
}
else if(choice.equals("B") || choice.equals("b")){
pileB = pileB - amount;
System.out.println("A: "+pileA+" B: "+pileB+" C: "+pileC);
}
else if(choice.equals("C") || choice.equals("c")){
pileC = pileC - amount;
System.out.println("A: "+pileA+" B: "+pileB+" C: "+pileC);
}
else{
System.out.println("ERROR");
}
System.out.println(playerTwo+", which pile would you like to pick from?");
String choiceTwo = input.next();
System.out.println("How many cards would you like to take from pile "+choiceTwo+"?");
amount = input.nextInt();
if(choiceTwo.equals("A") || choice.equals("a")){
pileA = pileA - amount;
System.out.println("A: "+pileA+" B: "+pileB+" C: "+pileC);
}
else if(choiceTwo.equals("B") || choice.equals("b")){
pileB = pileB - amount;
System.out.println("A: "+pileA+" B: "+pileB+" C: "+pileC);
}
else if(choiceTwo.equals("C") || choice.equals("c")){
pileC = pileC - amount;
System.out.println("A: "+pileA+" B: "+pileB+" C: "+pileC);
}
else{
System.out.println("ERROR");
}
}
当您为播放器添加两个选项时,您似乎复制了 if 语句并忘记将小写选项的 choice.equals
转换为 choiceTwo.equals
if(choiceTwo.equals("A") || choiceTwo.equals("a")){
pileA = pileA - amount;
System.out.println("A: "+pileA+" B: "+pileB+" C: "+pileC);
}
else if(choiceTwo.equals("B") || choiceTwo.equals("b")){
pileB = pileB - amount;
System.out.println("A: "+pileA+" B: "+pileB+" C: "+pileC);
}
else if(choiceTwo.equals("C") || choiceTwo.equals("c")){
pileC = pileC - amount;
System.out.println("A: "+pileA+" B: "+pileB+" C: "+pileC);
}
您正在检查 player1 的输入,而您应该检查 player2 的小写输入:
if(choiceTwo.equals("A") || choice.equals("a")){ // should be || choiceTwo.equals("a")
pileA = pileA - amount;
System.out.println("A: "+pileA+" B: "+pileB+" C: "+pileC);
}
else if(choiceTwo.equals("B") || choice.equals("b")){ // should be || choiceTwo.equals("b")
pileB = pileB - amount;
System.out.println("A: "+pileA+" B: "+pileB+" C: "+pileC);
}
else if(choiceTwo.equals("C") || choice.equals("c")){ // should be || choiceTwo.equals("c")
pileC = pileC - amount;
System.out.println("A: "+pileA+" B: "+pileB+" C: "+pileC);
}
错误在
if(choiceTwo.equals("A") || choice.equals("a")){
pileA = pileA - amount;
你应该使用 choiceTwo 而不是第二个玩家的选择。