我有一個「線程異常」main「java.lang.IllegalStateException:Scanner closed」,無法插入。Java IntToBinary應用程序:線程「main」中的異常java.lang.IllegalStateException:掃描程序關閉
我差不多完成了這個應用程序(用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
{
\t private static int state;
\t private static String enterYOrN;
\t
\t public static void enterAnotherInt()
\t {
\t \t state = 0;
\t \t while (state < 2)
\t \t {
\t \t \t switch (state)
\t \t \t {
\t \t \t \t case 0:
\t \t \t \t \t System.out.println();
\t \t \t \t \t System.out.println("Do you want to enter another integer number? (y/n)");
\t \t \t \t \t break;
\t \t \t case 1:
\t \t \t \t goodBye();
\t \t \t \t break;
\t \t \t }
\t \t \t
\t \t \t enterYOrN = IntToBinary.in.next();
\t \t
\t \t \t if(enterYOrN.equalsIgnoreCase("y"))
\t \t \t {
\t \t \t \t state = 0;
\t \t \t \t IntToBinary.useIntToBinary();
\t \t \t }
\t \t \t else if(enterYOrN.equalsIgnoreCase("n"))
\t \t \t {
\t \t \t \t state++;
\t \t \t } \t
\t \t }
\t }
\t
\t public static void goodBye()
\t {
\t \t System.out.println("Thank you for using Int To Binary.");
\t \t System.out.println("Good bye.");
\t \t
\t \t IntToBinary.in.close();
\t }
}
我剛剛測試了while,你的權利,「線程中的異常」main「java.util.InputMismatchException」。什麼是解決這個問題的好方法? – user3735449
我已經添加了解決方案。也許,你只需刷新頁面來反映我的編輯。 –
返回後再見()解決了我的主要問題。但是,現在「感謝您使用Int To Binary」。 「再見。」,在輸出中顯示兩次。爲了解決這個問題,可以僅僅爲了關閉和移動goodby()來做一個類,來解決這個問題? – user3735449