java - Java 扫描器有时需要输入两次
问题描述
不知道发生了什么,但是当我使用扫描仪读取 int 时,它会读取它,然后重复“问题”语句。就像程序不会承认输入一样。这也是完全随机的,不会每次都发生。有时它只会问一次,有时它最多会问2次。任何人都可以帮助我对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
在输入 = 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。
推荐阅读
- sql-server - SQL Server docker 无法使用撰写配置在 Docker Mac 桌面上启动
- python - 贝叶斯网络:与 R 相比,Python 中的结构学习非常慢
- database - 项目正在运行但数据库连接未找到
- r - 在 X 轴上创建列标题的折线图
- c# - 为什么我的并行任务代码不能达到 100% 的 CPU 利用率?
- c++ - 我可以在没有 Xcode 的 Mac 上使用 UnrealEngine 和 C++
- reactjs - 使用反应时将3d模型导入巴比伦
- postgresql - PL/pgSQL 函数中的语句是否按顺序运行?
- android - RXAndroid/RXJava2:链接三个改造请求并再次重复相同的过程
- python-requests - 我应该如何实现对响应生效的 Auth 处理程序?