簡體   English   中英

Java while循環無法按預期工作

[英]Java while loop doesn't work as intended

public static void main(String[] args) {
    boolean run = true;
    while (run) {
        Scanner kb = new Scanner(System.in);
        System.out.println("Welcome to MetricMan - the best converter in town!");
        System.out.println("Choose and option:");
        System.out.println("A) Fahrenheit to Celsius");
        System.out.println("B) Celcius to Fahrenheit");
        System.out.println("C) Miles to Kilometers");
        System.out.println("D) Kilometers to Miles");
        System.out.println("E) Inches to Centimeters");
        System.out.println("F) Centimeters to Inches");
        System.out.println("X) Quit the program");

        System.out.print("Your choice > ");
        String choice = kb.nextLine();

        double output; int input;
        System.out.print("Enter the number of ");
        switch (choice) { // do actions according to the user's choice. convert and display
        case "A":
            System.out.print("Farenheit > ");
            input = kb.nextInt();
            output = convertFtoC(input);
            System.out.println(input+" farenheit is "+output+" celsius");
            break;
        case "B":
            System.out.print("Celsius > ");
            input = kb.nextInt();
            output = convertCtoF(input);
            System.out.println(input+" celsius is "+output+" farenheit");
            break;
        case "C":
            System.out.print("miles > ");
            input = kb.nextInt();
            output = convertMtoK(input);
            System.out.println(input+" miles is "+output+" kilometres");
            break;
        case "D":
            System.out.print("kilometres > ");
            input = kb.nextInt();
            output = convertKtoM(input);
            System.out.println(input+" kilometres is "+output+" miles");
            break;
        case "E":
            System.out.print("inches > ");
            input = kb.nextInt();
            output = convertItoC(input);
            System.out.println(input+" inches is "+output+" centimeters");
            break;
        case "F":
            System.out.print("centimeters > ");
            input = kb.nextInt();
            output = convertCtoI(input);
            System.out.println(input+" centimeters is "+output+" inches");
            break;
        case "X":
            System.out.println(); System.out.println();
            System.out.println("May the odds be ever metric!");
            run = false;
            break;
        }
        System.out.println();
        kb.nextLine();
    }
} // end of main()

這是我的主要方法。 從代碼中可以看出,輸入X后,代碼應立即停止運行,但是實際輸出為

...
Your choice > X
Enter the number of 

May the odds be ever metric!

在實際的預期輸出之前有一個“輸入數字”,我認為沒有理由在其中。 誰能幫忙解決此問題?

每次循環時都會出現該行。 解決此問題的一種方法是在每個case語句(X除外)中進行該調用。

在你的代碼中

    System.out.print("Enter the number of ");

    switch (choice) { // do actions according to the user's choice. convert and display

因此,您基本上是在切換之前打印此內容,因此可以預期輸出,您可能希望在內部實例而不是外部切換中調用它,當它的x退出時。

您正在做System.out.print("Enter the number of "); 在每種情況下。所以這就是為什么當您輸入X時要進行打印,因此請嘗試放入System.out.print("Enter the number of "); if(check if string entered is not X)內部if(check if string entered is not X)如下

if(!choice.equals("X")){
System.out.print("Enter the number of ");
}

要么

System.out.print("Enter the number of "); 在每個開關盒內,如@Tanmoy所指

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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