【发布时间】:2015-08-03 10:31:14
【问题描述】:
我正在做一个我在互联网上找到的模拟游戏“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");
}
}
【问题讨论】:
-
你试过调试了吗?
-
您在第二个块中使用的是choice.equals 而不是choiceTwo.equals
-
@Shekhar 是的,我以为我做到了,但我错过了一些非常明显的东西。不过谢谢
标签: java string if-statement while-loop