簡體   English   中英

為什么我的代碼不打印任何東西? 我的循環有問題嗎?

[英]Why is my code not printing anything? Is there something wrong with my loops?

我在一個名為 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);
    }
}

}

但是當我運行它時,它什么也沒打印。 它只是說<terminated>並且 output 是空白的。 我嘗試修復它並一遍又一遍地運行它,但結果是一樣的。 我的代碼有問題嗎? 如果是這樣,我能做些什么來解決它? 我將如何防止這種情況再次發生?

它說<terminated>因為您的輸入不匹配任何if條件,所以程序終止時沒有 output。 在主 function 的末尾添加一個簡單的打印以報告沒有匹配項,例如:

System.out.println("Input doesn't match any result");

也正如評論中提到的,在開頭解析輸入然后處理它。 我重構了你的代碼:

// ...
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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM