我正在制作一个玩Go Fish游戏的程序。循环一次后,除scan.next()
之外的所有内容都有效。
嗯,有时它会起作用,有时则不起作用。这是代码:
System.out.println(compHand.showHand());
while (!(deck.isEmpty())) {
y = 0;
while (x == 1) {
System.out.println("\nYour hand:" + userHand.showHand()+ "\nYour turn. What number do you want to match?");
guess = scan.next();
if (compHand.checkHand(guess)) {
System.out.println("Darn...here you go.");
userHand.removeNum(guess);
compHand.removeNum(guess);
userHand.showHand();
uP++;
}
else {
System.out.println("Nope! Type '1' to go fish!");
y = scan.nextInt();
if (y == 1) {
userHand.goFish();
System.out.println(userHand.showHand());
}
y = 0;
}
guess = "";
x--;
}
while (x == 0) {
System.out.println("Do you have any " + compHand.ask() + "s?");
ans = scan.next();
if (!(ans.contains("go"))) {
System.out.println("Yay!");
userHand.removeNum(ans);
compHand.removeNum(ans);
cP++;
x++;
}
else {
System.out.println("Aww...darn.");
compHand.goFish();
x++;
}
}
System.out.println("Computer's points so far: " + cP + "\nYour points so far: " + uP + "\n");
}
}
所以它第一次循环来做用户的手,它的工作原理。然后它适用于计算机,但如果计算机必须去钓鱼。当它循环回到用户手上时,它会跳过guess = scan.next();
。
我不知道该怎么做......
答案 0 :(得分:-1)
问题来自缓冲区。你必须免费它。为此,将一个scan.nextLine()放在循环的右括号之前,它将释放缓冲区。然后,您就可以输入输入。