[英]Why is my code not printing anything? Is there something wrong with my loops?
I have the following code in a project called PostOffice:我在一个名为 PostOffice 的项目中有以下代码:
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";
if(3.5<Integer.parseInt(determineinput[0]) && Integer.parseInt(determineinput[0])<4.25){
if(3.5<Integer.parseInt(determineinput[1]) && Integer.parseInt(determineinput[1])<6){
if(0.007<Double.parseDouble(determineinput[2]) && Double.parseDouble(determineinput[2])<0.016){
System.out.println(regular);
}
}
}
else if(4.25<Integer.parseInt(determineinput[0]) && Integer.parseInt(determineinput[0])<6){
if(6<Integer.parseInt(determineinput[1]) && Integer.parseInt(determineinput[1])<11.5){
if(0.007<Double.parseDouble(determineinput[2]) && Double.parseDouble(determineinput[2])<0.016){
System.out.println(large);
}
}
}
else if(3.5<Integer.parseInt(determineinput[0]) && Integer.parseInt(determineinput[0])<6.125){
if(5<Integer.parseInt(determineinput[1]) && Integer.parseInt(determineinput[1])<11.5){
if(0.25<Double.parseDouble(determineinput[2]) && Double.parseDouble(determineinput[2])<0.5){
System.out.println(envelope);
}
}
}
else if(6.125<Integer.parseInt(determineinput[0]) && Integer.parseInt(determineinput[0])<24){
if(11<Integer.parseInt(determineinput[1]) && Integer.parseInt(determineinput[1])<18){
if(0.25<Double.parseDouble(determineinput[2]) && Double.parseDouble(determineinput[2])<0.5){
System.out.println(largeenvenlope);
}
}
}
else if(Integer.parseInt(determineinput[0])<6.125 || Integer.parseInt(determineinput[0])>24){
if(Integer.parseInt(determineinput[1])<11 || Integer.parseInt(determineinput[1])>18){
if(Double.parseDouble(determineinput[2])<0.25 || Double.parseDouble(determineinput[2])>0.5){
if((Integer.parseInt(determineinput[0])*2+Integer.parseInt(determineinput[1])*2)<=84){
System.out.println(packagee);
}
}
}
}
else if(Integer.parseInt(determineinput[0])<6.125 || Integer.parseInt(determineinput[0])>24){
if(Integer.parseInt(determineinput[1])<11 || Integer.parseInt(determineinput[1])>18){
if(Double.parseDouble(determineinput[2])<0.25 || Double.parseDouble(determineinput[2])>0.5){
if((Integer.parseInt(determineinput[0])+Integer.parseInt(determineinput[1]))>84 && (Integer.parseInt(determineinput[0])+Integer.parseInt(determineinput[1]))<130){
System.out.println(largepackage);
}
}
}
}
if((Integer.parseInt(determineinput[0])<3.5 || Integer.parseInt(determineinput[0])>24) && (Double.parseDouble(determineinput[2])<0.07 && Double.parseDouble(determineinput[2])>0.25) && (Integer.parseInt(determineinput[1])<3.5 && Integer.parseInt(determineinput[1])>18)){
System.out.println(unmailable);
}
}
} }
But when I run it, it prints nothing.但是当我运行它时,它什么也没打印。 It just says <terminated>
and the output is blank.它只是说<terminated>
并且 output 是空白的。 I tried fixing it and ran it several times over and over again, but the result is the same.我尝试修复它并一遍又一遍地运行它,但结果是一样的。 Is there something wrong with my code?我的代码有问题吗? If so, what can I do to resolve it?如果是这样,我能做些什么来解决它? And how would I prevent this from happening again?我将如何防止这种情况再次发生?
It says <terminated>
because your input doesn't match any if
condition, so the program terminates with no output.它说<terminated>
因为您的输入不匹配任何if
条件,所以程序终止时没有 output。 Add a simple print at the end of your main function to report when there is no match like:在主 function 的末尾添加一个简单的打印以报告没有匹配项,例如:
System.out.println("Input doesn't match any result");
Also as mentioned in the comment, parse the input at the beginning and then process it.也正如评论中提到的,在开头解析输入然后处理它。 I refactored your code:我重构了你的代码:
// ...
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");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.