Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java中的转换程序错误_Java_Error Handling - Fatal编程技术网

Java中的转换程序错误

Java中的转换程序错误,java,error-handling,Java,Error Handling,今天早上我很无聊,所以我决定制作一个程序,用户可以转换速度、质量、温度或长度。我用扫描仪和开关操作员来做这件事。基本上,我将用户类型输入到他们想要转换的内容中,然后switch操作符根据答案运行某个方法。到目前为止,我只做了大量的工作,这就是为什么其他方法都被注释掉了。代码和输出如下: 代码: 输出: Would you like to convert: Mass, Temperature, Length, or Speed? /*This wasn't in the output, but

今天早上我很无聊,所以我决定制作一个程序,用户可以转换速度、质量、温度或长度。我用扫描仪和开关操作员来做这件事。基本上,我将用户类型输入到他们想要转换的内容中,然后switch操作符根据答案运行某个方法。到目前为止,我只做了大量的工作,这就是为什么其他方法都被注释掉了。代码和输出如下:

代码:

输出:

Would you like to convert: Mass, Temperature, 
Length, or Speed?
/*This wasn't in the output, but I just want to mention that the line with "Mass" was my input.*/
Mass
1. kg to lbs
2. lbs to kg
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at Convertions.main(Convertions.java:14)

读取用户的第一个输入后,您将关闭链接到标准输入流的
扫描仪

scan.close();
这会导致stdin关闭,并且输入流的任何后续使用都会因异常而失败


要解决此问题,应确保仅在不需要更多输入时关闭
扫描仪。

如此简单,删除该行

scan.close();//remove this
由于您在获得第一次输入后即将关闭扫描仪,因此下次无法访问相同的输入

scan.close();//remove this