Java扫描仪有时需要输入两次



不确定发生了什么,但当我使用扫描仪读取int时,它会读取它,然后重复"问题";陈述这就像程序不会确认输入一样。这也是完全随机的,并不是每次都会发生。有时它只会问一次,而其他时候它最多会问两次。有人能帮我吗?我对Java有点陌生,已经被困了大约2个小时了?

int gameCalc() {
System.out.print("0:Rock, 1:Spock, 2:Paper, 3:Lizard, 4:Scissors: ");     
playerNumber = scan.nextInt();
computerNumber = rand.nextInt(5); 

mod = playerNumber - computerNumber;

playerString = resultCalc(playerNumber);
computerString = resultCalc(computerNumber);

if (playerNumber == computerNumber) {
System.out.println("nTie!");
System.out.println(playerString + resultString + computerString);
return 0;
}
else if (Math.floorMod(mod, 5) < 3){
System.out.println("nPlayer Wins This Round!");
System.out.println(playerString + resultString + computerString);
return 1;
}    
else if (Math.floorMod(mod, 5) > 3) {
System.out.println("nComputer Wins This Round!");
System.out.println(playerString + resultString + computerString);
return 2;
}
else{return 5;}
} 
String resultCalc(int i) {
switch(i) {
case 0:
playerChoice[0] += 1;
return "Rock";
case 1:
return "Spock";
case 2:
return "Paper";
case 3:
return "Lizard";
case 4:
return "Scissors";
default:
return "Invalid Input";
}
}
void gameLoop() {
int pWins, cWins;
int tpWins, tcWins, tTies;
int n, temp;
String cont;
boolean val;
val = true;
pWins = cWins = 0;
tpWins = tcWins = tTies = 0;

while (true) {
System.out.print("Best Of One Press: 1. Best Of Three Press: 2. Best Of Five Press: 3. View Stats Press: 4: ");

n = scan.nextInt();
switch(n) {
case 1:
pWins = cWins = 0;
while (val) {
temp = this.gameCalc();

if (temp == 0) {tTies += 1;}
if (temp == 1) {pWins += 1;}
if (temp == 2) {cWins += 1;}
if (cWins == 1) {
System.out.println("nComputer Wins The Match!");
val = false;
}
if (pWins == 1) {
System.out.println("nPlayer Wins The Match!");
val = false;
}
}
break;
case 2:
pWins = cWins = 0;
while (val) {
temp = this.gameCalc();

if (temp == 0) {tTies += 1;}
if (temp == 1) {pWins += 1;}
if (temp == 2) {cWins += 1;}

if (cWins == 2) {
System.out.println("nComputer Wins The Match " + cWins + " to " + pWins);
val = false;
}
if (pWins == 2) {
System.out.println("nPlayer Wins The Match " + pWins + " to " + cWins);
val = false;
}
}
break;
case 3:
pWins = cWins = 0;
while (val) {
temp = this.gameCalc();

if (temp == 0) {tTies += 1;}
if (temp == 1) {pWins += 1;}
if (temp == 2) {cWins += 1;}
if (cWins == 3) {
System.out.println("nComputer Wins The Match " + cWins + " to " + pWins);
val = false;
}
else if (pWins == 3) {
System.out.println("nPlayer Wins The Match " + pWins + " to " + cWins);
val = false;
}
}
break;
case 4:
System.out.println("Stats: nTotal Player Match Wins: " + tpWins);
System.out.println("Total Computer Match Wins: " + tcWins);
System.out.println("Total Round Ties: " + tTies);
for (int element: this.playerChoice) {
System.out.println(element);
}       
continue;
default:
System.out.println("error");
}    
System.out.println("Enter N To Quit Or Y To Play Again: ");
cont = scan.next();

if (cont.equals("N") | cont.equals("n")) {
scan.close();
break;
}
else {val = true; continue;}    
}

scan.close();
}
}

public class JacobCardosoRPSLZ {
public static void main(String[] args) {
Calculations myCalc = new Calculations();

myCalc.gameLoop();

}
}

如果玩家选择纸张(2(,计算机选择蜥蜴(4(,则:

mod = -2
Math.floorMod(-2, 5) == 3

当input=3时,calc循环返回5(因为3个选项中没有一个触发;2不等于4,mod既不高于也不低于3。

5显然是一个不可能的"uhoh",但你的代码的其余部分没有检查它。这是你选择的大约10种有问题的代码样式之一。根据一般经验,"在意外/看似不可能的情况下返回奇怪的结果"是一个非常糟糕的主意。你永远不会检查这些。引发异常。只是throw new IllegalStateException("I really did not think we would ever get here");是好的。然后你就会知道,而不是花2个小时,然后在SO上询问。

最新更新