代码是打印错误,尽管我确信我做得对.怎么了



以下是我有问题的代码:

package project;
import java.util.Scanner;
public class PostOffice {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("User input: ");
String input = sc.nextLine();
String[] determineinput = input.split(",");
String regular = "Regular Postcard";
String large = "Large Postcard";
String envelope = "Envelope";
String largeenvenlope = "Large Envelope";
String packagee = "Package";
String largepackage = "Large Package";
String unmailable = "Unmailable";
double height = Double.parseDouble(determineinput[0]);
double length = Double.parseDouble(determineinput[1]);
double thickness = Double.parseDouble(determineinput[2]);
if (3.5 < height && height < 4.25) {
if (3.5 < length && length < 6) {
if (0.007 < thickness && thickness < 0.016) {
System.out.println(regular);
}
}
} else if (4.25 < height && height < 6) {
if (6 < length && length < 11.5) {
if (0.007 < thickness && thickness < 0.016) {
System.out.println(large);
}
}
} else if (3.5 < height && height < 6.125) {
if (5 < length && length < 11.5) {
if (0.25 < thickness && thickness < 0.5) {
System.out.println(envelope);
}
}
} else if (6.125 < height && height < 24) {
if (11 < length && length < 18) {
if (0.25 < thickness && thickness < 0.5) {
System.out.println(largeenvenlope);
}
}
} else if (height < 6.125 || height > 24) {
if (length < 11 || length > 18) {
if (thickness < 0.25 || thickness > 0.5) {
if ((height * 2 + length * 2) <= 84) {
System.out.println(packagee);
}
}
}
} else if (height < 6.125 || height > 24) {
if (length < 11 || length > 18) {
if (thickness < 0.25 || thickness > 0.5) {
if ((height + length) > 84 && (height + length) < 130) {
System.out.println(largepackage);
}
}
}
}
if ((height < 3.5 || height > 24)
&& (thickness < 0.07 || thickness > 0.25)
&& (length < 3.5 || length > 18)) {
System.out.println(unmailable);
}
System.out.println("Input doesn't match any result");
}
} 

当我运行这个特定的代码时,我会得到这个错误:

Exception in thread "main" java.util.NoSuchElementException: No line found 
at java.util.Scanner.nextLine(Scanner.java:1540)
at project.PostOffice.main(PostOffice.java:8)

怎么了?我包含了匹配初始化变量的条件,并在条件返回true时打印该变量。该系统确实打印";用户输入:";但在它下面,错误弹出。我摆脱了可能成为问题的东西,但它没有改变。我确信我为系统编写了非常明确和简单的代码,但它一直在抱怨。我看不出有其他办法可以解决这个问题。我的for循环有什么问题吗?还是别的什么?

(很抱歉,这真的应该在评论中出现,但我的声誉还不够高,无法做到这一点(。

我试过在IntelliJ上运行您的代码,它似乎对我有效(我使用了4、4、4的输入。您的错误是在用户输入行打印后立即出现的,还是您可以先输入一些数字?如果是后者,您的意见是什么?

这与你的问题无关,但可能会让你的代码更容易阅读:如果变量名由多个单词组成,请尝试使用驼色大小写(将每个单词中第一个字母大写,第一个字母除外,例如largevelope变为largeEnvelope(。我很喜欢你没有使用隐晦缩写的名字:(

相关内容

最新更新