Java IntToBinary 应用程序:线程中出现异常 "main" java.lang.IllegalStateException:扫描程序已关闭

Java IntToBinary app: Exception in thread "main" java.lang.IllegalStateException: Scanner closed

我有一个 "Exception in thread "main" java.lang.IllegalStateException: Scanner closed" 我无法插入。

我即将完成此应用程序(用 Java 编写)。它将 int 转换为二进制,反转二进制,并将反转的二进制转换为十进制。它运行,没有错误,但是当它终止时:

Exception in thread "main" java.lang.IllegalStateException: Scanner closed
at java.util.Scanner.ensureOpen(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at intToBinary.AnotherOrCloseIntToBinary.enterAnotherInt(AnotherOrCloseIntToBinary.java:24)
at intToBinary.IntToBinary.useIntToBinary(IntToBinary.java:48)
at intToBinary.AnotherOrCloseIntToBinary.enterAnotherInt(AnotherOrCloseIntToBinary.java:29)
at intToBinary.IntToBinary.useIntToBinary(IntToBinary.java:48)
at intToBinary.AnotherOrCloseIntToBinary.enterAnotherInt(AnotherOrCloseIntToBinary.java:29)
at intToBinary.IntToBinary.useIntToBinary(IntToBinary.java:48)
at intToBinary.IntToBinary.main(IntToBinary.java:18) 

欢迎就应用程序的任何部分提出任何建议。这样做不到一年,所以我一直在学习。但主要是,我想解决以上问题。

谢谢。

 <!-- language: lang-java -->

package intToBinary;

import java.util.Scanner;

public class IntToBinary 
{

    private static int invertedBinaryDecimal;
    private static int _n;
    private static String binaryString;
    private static String invertedBinaryString;

    static Scanner in = new Scanner(System.in);

    public static void main(String[] args) 
    {
        greeting();
        useIntToBinary();
    }

    public static void greeting()
    {
        System.out.println("*Int To Binary*");
    }

    public static void useIntToBinary()
    {
        System.out.println("Please enter an integer number: ");
        while ((!in.hasNextInt())) 
        {
            System.out.print("You did not enter an integer number.  An integer number(whole
            numbers or counting numbers) is a    number like: 1, 2, 3...");
            System.out.print("Please enter an integer number: ");
            in.nextInt();
        }

        _n = in.nextInt();

        getBinary();
        getInvertedBinary();
        getIntegerComplement();

        System.out.println("You entered integer number: " + _n );
        System.out.println("In binary(base 2): " + binaryString);
        System.out.println("The inversion of this binary(base 2): " + invertedBinaryString);
        System.out.println("The inverted binary(base 2) in decimal(base 10) is: " +
        invertedBinaryDecimal);
        AnotherOrCloseIntToBinary.enterAnotherInt();
    }

    public static void getBinary()
    {
        binaryString = Integer.toBinaryString(_n);          
    }

    public static void getInvertedBinary()
    {
        invertedBinaryString = binaryString.replaceAll("0", "x").replaceAll("1",
        "0").replaceAll("x", "1");
    }

    public static void getIntegerComplement()
    {
        invertedBinaryDecimal = Integer.parseInt(invertedBinaryString, 2);  
    }
}

package intToBinary;

public class AnotherOrCloseIntToBinary 
{
 private static int state;
 private static String enterYOrN;
 
  public static void enterAnotherInt() 
  {
   state = 0;
   while (state < 2) 
   {
    switch (state) 
    {
     case 0: 
      System.out.println();
      System.out.println("Do you want to enter another integer number? (y/n)");
      break;
       case 1:
        goodBye();
        break;
    }
              
    enterYOrN = IntToBinary.in.next();
      
    if(enterYOrN.equalsIgnoreCase("y")) 
    {
     state = 0;
     IntToBinary.useIntToBinary();
    }
    else if(enterYOrN.equalsIgnoreCase("n")) 
    {
     state++;
    } 
   }
  }
  
  public static void goodBye()
  {
      System.out.println("Thank you for using Int To Binary.");
      System.out.println("Good bye.");
      
      IntToBinary.in.close();
  }
}

您正在尝试从关闭的扫描器中读取:

         switch (state) 
         {
            case 0: 
                System.out.println();
                System.out.println("Do you want to enter another integer number? (y/n)");
                break;
            case 1:
                AnotherOrCloseIntToBinary.goodBye(); // here you close the scanner
                break;
         }

         enterYOrN = IntToBinary.in.next(); // here you try to read from a 
                                            // closed scanner

goodBye() 方法的调用应该只在 main 方法的最后一行。

您的代码有两个问题。您已经确定的一个正在尝试从已经关闭的 Scanner 中读取。要在您调用 AnotherOrCloseIntToBinary.goodBye() as

后立即从 enterAnotherInt() 简单地修复它 return
     switch (state) 
     {
        case 0: 
            System.out.println();
            System.out.println("Do you want to enter another integer number? (y/n)");
            break;
        case 1:
            AnotherOrCloseIntToBinary.goodBye();
            return; // instead of break;
     }

您还没有意识到的第二个问题是 while 循环中的 in.nextInt() 调用。

    while ((!in.hasNextInt())) 
    {
        System.out.print("You did not enter an integer number.  An integer number(whole numbers or counting numbers) is a    number like: 1, 2, 3...");
        System.out.print("Please enter an integer number: ");
        in.nextInt(); // use in.next() here
    }

要查看失败原因,只需在代码提示输入整数时输入非数字输入即可。您会收到 InputMismatchException。它失败的原因是你已经在循环中,因为输入的不是数字,但你的代码继续并在扫描仪上触发 nextInt() 调用,这必然会失败。

要修复它,只需使用 in.next() 而不是 nextInt()