线程" main"中的例外情况java.util.NoSuchElementException扫描程序错误

时间:2015-03-17 02:52:11

标签: java poker nosuchelementexception

我收到此错误,无法弄清楚如何修复它。班级导师无法弄明白。这是一个扑克计划,可以让用户更换最多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

        }

    }
}

这是它正在调用的方法,即真正的主要代码

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;
    }
}

还有其他课程,但它们不应影响扫描仪。 这是错误

   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)

1 个答案:

答案 0 :(得分:0)

后按下初始回车符
System.out.println("Enter 2 to play a hand or 1 to quit the game: ");
while ((game = scan.nextInt()) == 2)

从不消耗,并且在调用playHand时仍然在输入上。

所以在致电playHand致电scan.nextLine();

之前