gpt4 book ai didi

JAVA:为什么我不断收到这些运行时错误?

转载 作者:行者123 更新时间:2023-12-01 18:10:55 25 4
gpt4 key购买 nike

写了一个roshambo程序,运行一次,运行正常,再次运行,弹出以下错误:

RoShamBo!

  1. 玩游戏
  2. 显示分数
  3. 退出

1

你选择哪一个?

  1. 摇滚
  2. 剪刀

2

你选择哪一个?

  1. 摇滚
  2. 剪刀

输入无效,请重试。

Exception in thread "main" java.util.NoSuchElementException

at java.util.Scanner.throwFor(Unknown Source)

at java.util.Scanner.next(Unknown Source)

at RoShamBo.getUserChoice(RoShamBo.java:82)

at RoShamBo.winner(RoShamBo.java:112)


at RoShamBo.main(RoShamBo.java:27)

不确定如何处理这些类型的错误,这是我第一次使用方法,所以我认为这与我如何调用每个方法有关?请帮忙。

提前致谢。

import java.util.Scanner;

public class RoShamBo
{
public static void main(String[] args)
{
System.out.println("RoShamBo!");
System.out.println("1. Play Game");
System.out.println("2. Show Score");
System.out.println("3. Quit");
Scanner userInput = new Scanner(System.in);

if(userInput.hasNextInt())
{
int userIn = userInput.nextInt();
if(userIn == 1)
{
getUserChoice();
getCompChoice();
winner();
}
else if(userIn == 2)
{
scores();
}
else if(userIn == 3)
{
System.out.println("Qutiing: Final Score was: ");
scores();
userInput.close();
}
else
{
System.out.println("Invalid Entry, Please Try Again.");
userInput.next();
}
}
else
{
System.out.println("Invalid Entry, Please Try Again.");
userInput.next();
}
}
public static String getUserChoice()
{
// ask player for a move : playerMove
System.out.println("Which do you choose?");
System.out.println("1. Rock");
System.out.println("2. Paper");
System.out.println("3. Scissors");

Scanner input = new Scanner(System.in);
String userChoice = " ";

if (input.hasNextInt())
{
int userInput = input.nextInt();
switch(userInput)
{
case 1:
userChoice = "Rock";
break;
case 2:
userChoice = "Paper";
break;
case 3:
userChoice = "Scissors";
break;
}

}
else
{
System.out.println("Invalid Entry, Please Try Again.");
input.next();
}
input.close();
return userChoice;
}
private static String getCompChoice()
{
//Method for getting Computers move
int compChoice = (int) ( 1 + (Math.random() * 3));
String compMove = " ";
switch(compChoice)
{
case 1:
compMove = "Rock";
break;
case 2:
compMove = "Paper";
break;
case 3:
compMove = "Scissors";
break;
}

return compMove;
}

public static String winner()
{
String winnerIs = " ";
String comp = getCompChoice();
String user = getUserChoice();
if(user.equals("Rock") && comp.equals("Scissors") ||
user.equals("Paper") && comp.equals("Rock") ||
user.equals("Scissors") && comp.equals("Paper"))
{
System.out.println("You Win!");
}
else
{
System.out.println("You Lose");
}
return winnerIs;
}
public static void scores()
{
int compCount = 0;
int userCount = 0;
if (winner().equals("You Win!"))
{
userCount++;
}
else
{
compCount++;
}
System.out.println("Player = " + userCount );
System.out.println("Computer = " + compCount );
}
}

最佳答案

大概在这里

 {
System.out.println("Invalid Entry, Please Try Again.");
input.next();
}

您正在执行 next() 而不检查 has*()

关于JAVA:为什么我不断收到这些运行时错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33075661/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com