Isa*_*ith 8 java exception java.util.scanner
每当我运行它,该chooseCave()功能与in.nextInt().当我选择洞穴时,消息会以2秒的间隔弹出,然后一旦超过该部分,它就会给我错误:
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Unknown Source)
at Dragon.main(Dragon.java:81)
Run Code Online (Sandbox Code Playgroud)
我曾尝试hasNextLine()和hasNextInt(),当我使用while hasNextLine()的main方法,我得到一吨多的错误.当我while hasNextInt()在chooseCave()方法中使用时,它不接受我的输入.
当我if hasNextInt()在chooseCave()方法中使用时,它不接受我对playAgain字符串的输入,并直接进入另一个游戏,但随后hasNextInt()布尔返回false并且无限地发送"哪个洞穴......".
我已经完成了错误报告以及类似问题的Java-docs和Stack Overflow.请帮忙.
import java.util.Scanner;
public class Dragon {
public static void displayIntro() {
System.out.println("You are in a land full of dragons. In front of you, ");
System.out.println("You see two caves. In one cave, the dragon is friendly");
System.out.println("and will share his treasure with you. The other dragon");
System.out.println("is greedy and hungry, and will eat you on sight");
System.out.println(' ');
}
public static int chooseCave() {
Scanner in = new Scanner(System.in);
int cave = 0;
while (cave != 1 && cave != 2) {
System.out.println("Which cave will you go into? (1 or 2)");
cave = in.nextInt();
}
in.close();
return cave;
}
public static void checkCave(int chosenCave) {
System.out.println("You approach the cave...");
try
{
// Sleep at least n milliseconds.
// 1 millisecond = 1/1000 of a second.
Thread.sleep( 2000 );
}
catch ( InterruptedException e )
{
System.out.println( "awakened prematurely" );
}
System.out.println("It is dark and spooky...");
try
{
// Sleep at least n milliseconds.
// 1 millisecond = 1/1000 of a second.
Thread.sleep( 2000 );
}
catch ( InterruptedException e )
{
System.out.println( "awakened prematurely" );
}
System.out.println("A large dragon jumps out in front of you! He opens his jaws and...");
try
{
// Sleep at least n milliseconds.
// 1 millisecond = 1/1000 of a second.
Thread.sleep( 2000 );
}
catch ( InterruptedException e )
{
System.out.println( "awakened prematurely" );
}
double friendlyCave = Math.ceil(Math.random() * 2);
if (chosenCave == friendlyCave) {
System.out.println("Gives you his treasure!");
}
else {
System.out.println("Gobbles you down in one bite!");
}
}
public static void main(String[] args) {
Scanner inner = new Scanner(System.in);
String playAgain = "yes";
boolean play = true;
while (play) {
displayIntro();
int caveNumber = chooseCave();
checkCave(caveNumber);
System.out.println("Do you want to play again? (yes or no)");
playAgain = inner.nextLine();
if (playAgain == "yes") {
play = true;
}
else {
play = false;
}
}
inner.close();
}
}
Run Code Online (Sandbox Code Playgroud)
Rei*_*eus 13
你关闭第二个Scanner关闭底层的东西InputStream,因此第一个Scanner不能再读取相同InputStream的NoSuchElementException结果.
解决方案:对于控制台应用程序,使用单个Scanner来读取System.in.
旁白:如前所述,请注意Scanner#nextInt不要使用换行符.在尝试使用之前nextLine再次呼叫之前,请确保消耗这些消耗Scanner#newLine().
请参阅:不要在单个InputStream上创建多个缓冲包装器
该nextInt()方法离开\n(结束行)符号并立即被拾取nextLine(),跳过下一个输入.你想要做的是nextLine()用于所有事情,并在以后解析它:
String nextIntString = keyboard.nextLine(); //get the number as a single line
int nextInt = Integer.parseInt(nextIntString); //convert the string to an int
Run Code Online (Sandbox Code Playgroud)
这是迄今为止避免问题的最简单方法 - 不要混淆你的"下一步"方法.仅使用nextLine(),然后解析int或单独的单词.
此外,Scanner如果您只使用一个终端进行输入,请确保只使用一个.这可能是例外的另一个原因.
最后注意:将a String与.equals()函数进行比较,而不是==运算符.
if (playAgain == "yes"); // Causes problems
if (playAgain.equals("yes")); // Works every time
Run Code Online (Sandbox Code Playgroud)