Java 程序中途崩溃,不知道为什么

Java 程序中途崩溃,不知道为什么,java,exception,crash,Java,Exception,Crash,这是我得到的,看看你能找到什么。eclipse说一切都很好,但当我运行它时,我会输入5个数字,然后它会询问从最高到最低的两行,反之亦然,然后在我输入答案之前崩溃。我不知所措 这些就是我看到的错误。线程“main”java.util.NoSuchElementException中出现异常 位于java.util.Scanner.throwFor(Scanner.java:907) 下一步(Scanner.java:1416) 在monty.intarray.main(intarray.java:2

这是我得到的,看看你能找到什么。eclipse说一切都很好,但当我运行它时,我会输入5个数字,然后它会询问从最高到最低的两行,反之亦然,然后在我输入答案之前崩溃。我不知所措

这些就是我看到的错误。线程“main”java.util.NoSuchElementException中出现异常 位于java.util.Scanner.throwFor(Scanner.java:907) 下一步(Scanner.java:1416) 在monty.intarray.main(intarray.java:25)

导入java.util.array;
导入java.util.Scanner;
公务舱{
公共静态void main(字符串[]args){
System.out.println(“输入一个非零的数字,然后按Enter键。这样做五次。”);
扫描仪输入1=新扫描仪(System.in);
int[]数组=新的int[5];
for(int whatever=0;whatever=0;a--){
系统输出打印(数组[a]+“”);
}
}
否则{
数组。排序(数组);

对于(intb=0;b当您调用input1.close时,它还将关闭输入流中的System.和扫描仪

要检查扫描仪是否仍然可用,您可以尝试:

System.out.println(System.in.available());
而且也没有办法重新打开系统

public void close() throws IOException --> Closes this input stream and releases any system resources associated with this stream. The general contract of close is that it closes the input stream. A closed stream cannot perform input operations and **cannot be reopened.**
因此,它抛出
NoTouchElementException

为了避免这种情况,请不要关闭input1,而是使用相同的扫描仪对象,根据需要接受尽可能多的输入,最后关闭扫描仪


希望这有帮助。

a guess but for(int b=0;b这帮了大忙。它现在可以工作了,我从最低到最高都能正确地获得输出,但即使程序运行正确,在最后也会出现此错误。线程“main”java.lang.ArrayIndexOutOfBoundsException:5在monty.intarray.main(intarray.java:41)中出现异常好的,我修正了,不管怎样,问题解决了,非常感谢扫描仪的回答!
public void close() throws IOException --> Closes this input stream and releases any system resources associated with this stream. The general contract of close is that it closes the input stream. A closed stream cannot perform input operations and **cannot be reopened.**