JAVA:为什么我总是遇到这些运行时错误?
编写了一个roshambo程序,运行了一次,工作正常,再次运行,出现了以下错误: 罗斯汉博JAVA:为什么我总是遇到这些运行时错误?,java,debugging,methods,runtime-error,Java,Debugging,Methods,Runtime Error,编写了一个roshambo程序,运行了一次,工作正常,再次运行,出现了以下错误: 罗斯汉博 玩游戏 显示分数 退出 一, 你选择哪一个 摇滚乐 纸 剪刀 二, 你选择哪一个 摇滚乐 纸 剪刀 输入无效,请重试 Exception in thread "main" java.util.NoSuchElementException at java.util.Scanner.throwFor(Unknown Source) at java.util.Scanner.next(Unknown Sou
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()
,但没有检查是否有*()
可能在这里
{
System.out.println("Invalid Entry, Please Try Again.");
input.next();
}
您正在执行一个
next()
,而没有检查has*()
,堆栈给出了一个提示
at java.util.Scanner.next(Unknown Source).
及
转到此处的文档:
您感兴趣的部分:
抛出:
NoTouchElementException-如果没有更多可用令牌
您可以调用.next()
但是你想读什么呢?输入缓冲区中是否有等待的内容?另外,请看我的评论,这意味着你的逻辑被破坏了。重新思考你的游戏逻辑,而不是修复它
给出一个完整的答案,让你的代码按照预期的方式运行,这意味着你要编写大部分代码,我不会那样做。相反,您应该应用人类已知的最强大的调试方法之一,并自己进行调试:
祝你好运,好好享受;) 堆栈给出了一个提示
at java.util.Scanner.next(Unknown Source).
及
转到此处的文档:
您感兴趣的部分:
抛出:
NoTouchElementException-如果没有更多可用令牌
您可以调用.next()
但是你想读什么呢?输入缓冲区中是否有等待的内容?另外,请看我的评论,这意味着你的逻辑被破坏了。重新思考你的游戏逻辑,而不是修复它
给出一个完整的答案,让你的代码按照预期的方式运行,这意味着你要编写大部分代码,我不会那样做。相反,您应该应用人类已知的最强大的调试方法之一,并自己进行调试:
祝你好运,好好享受;) 您熟悉循环的概念,对吗?据我所见,如果您希望用户输入被多次读取,那么至少应该在某个地方有一个代码>和
getCompChoice()
不存储结果,然后在winner()
中再次调用它们,用户必须播放两次!正如@Jens所说,如果不知道问题是如何产生的,就很难排除问题。如果您只想处理异常,可以使用try和catch语句。您熟悉循环的概念,对吗?据我所见,如果您希望用户输入被多次读取,那么至少应该在某个地方有一个代码>和getCompChoice()
不存储结果,然后在winner()
中再次调用它们,用户必须播放两次!正如@Jens所说,如果不知道问题是如何产生的,就很难排除问题。如果只想处理异常,可以使用try和catch语句。