线程异常 "main" java.util.NoSuchElementException 扫描器错误
Exception in thread "main" java.util.NoSuchElementException Scanner error
我遇到了这个错误,不知道如何解决。 class导师想不通。这是一个发牌的扑克程序,允许用户更换最多 4 张牌,然后对其进行分析。它的设计目的是让用户可以随心所欲地玩很多手牌,但是输入决定他们是否玩的扫描仪不工作,我不明白为什么。
这里是主要代码
import java.util.*;
public class Poker
{
public static void main(String[] args)
{
String game = "continue";
Run runpoker = new Run();
System.out.println("Enter 2 to play a hand or 1 to quit the game: ");
while (game == "continue")
{
game = ((Run)runpoker).playhand();//this is line 18
}
}
}
这里是调用的方法playhand,才是真正的主要代码
import java.util.Scanner;
public class Run
{
public String playhand()
{
int r = 1;
int limit = 0;
// create a new, shuffled Deck
Deck d = new Deck();
Hand h = d.deal();
h.sort();
System.out.println(h);
System.out.println("Which cards would you like to replace, up to 4");
System.out.println("Enter 0 when finished");
Scanner in = new Scanner(System.in);
r = in.nextInt();
while (r > 0 && r < 6 && limit < 4)
{
if (r > 0)
h.replace(d, r - 1);
limit++;
r = in.nextInt();
}
h.sort();
System.out.println(h);
String result = h.analyze();
System.out.println(result);
String answer;
System.out.println("Would you like to play again? Enter 'continue' to play again or 'quit' to stop playing: ");
in.close();
Scanner input = new Scanner(System.in);
answer = input.nextLine();//this is line 34
input.close();
return answer;
}
}
还有其他 classes 但它们应该不会影响扫描仪。
这是错误
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Unknown Source)
at Run.playhand(Run.java:34)
at Poker.main(Poker.java:18)
在
之后按下初始回车 return
System.out.println("Enter 2 to play a hand or 1 to quit the game: ");
while ((game = scan.nextInt()) == 2)
永远不会消耗,并且在调用 playHand
时仍在输入中。
所以在调用 playHand
之前调用 scan.nextLine();
我遇到了这个错误,不知道如何解决。 class导师想不通。这是一个发牌的扑克程序,允许用户更换最多 4 张牌,然后对其进行分析。它的设计目的是让用户可以随心所欲地玩很多手牌,但是输入决定他们是否玩的扫描仪不工作,我不明白为什么。
这里是主要代码
import java.util.*;
public class Poker
{
public static void main(String[] args)
{
String game = "continue";
Run runpoker = new Run();
System.out.println("Enter 2 to play a hand or 1 to quit the game: ");
while (game == "continue")
{
game = ((Run)runpoker).playhand();//this is line 18
}
}
}
这里是调用的方法playhand,才是真正的主要代码
import java.util.Scanner;
public class Run
{
public String playhand()
{
int r = 1;
int limit = 0;
// create a new, shuffled Deck
Deck d = new Deck();
Hand h = d.deal();
h.sort();
System.out.println(h);
System.out.println("Which cards would you like to replace, up to 4");
System.out.println("Enter 0 when finished");
Scanner in = new Scanner(System.in);
r = in.nextInt();
while (r > 0 && r < 6 && limit < 4)
{
if (r > 0)
h.replace(d, r - 1);
limit++;
r = in.nextInt();
}
h.sort();
System.out.println(h);
String result = h.analyze();
System.out.println(result);
String answer;
System.out.println("Would you like to play again? Enter 'continue' to play again or 'quit' to stop playing: ");
in.close();
Scanner input = new Scanner(System.in);
answer = input.nextLine();//this is line 34
input.close();
return answer;
}
}
还有其他 classes 但它们应该不会影响扫描仪。 这是错误
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Unknown Source)
at Run.playhand(Run.java:34)
at Poker.main(Poker.java:18)
在
之后按下初始回车 returnSystem.out.println("Enter 2 to play a hand or 1 to quit the game: ");
while ((game = scan.nextInt()) == 2)
永远不会消耗,并且在调用 playHand
时仍在输入中。
所以在调用 playHand
之前调用 scan.nextLine();