Java - 默认选项在 switch 中不起作用

Java - default option does not fuction in switch

你好,我编写了一个程序,使用开关将一种温度转换为另一种温度。

默认不生效:

当我输入一个有效的选项时,它工作得很好,但是当我选择无效的选择时,它会默认输出这些行:你输入了无效的选择,请再次选择并停止在不允许我选择其他选择的情况下工作。

早些时候我测试它时,默认允许我选择另一个选项。

import java.util.Scanner;

public class Tempature {
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        double fahrenheit,celcius,kelvin;
        System.out.println("Choose type of temperature:\nf. Fahrenheit\nc. Celcius\nk. Kelvin");
        String word = scan.nextLine();
        switch(word){
            case "f": System.out.println("Enter  Fahrenheit temperature: ");
            fahrenheit=scan.nextDouble();
            celcius = (fahrenheit-32) * 5/9;
            kelvin = (fahrenheit + 459.67) * 5/9;
            System.out.println("" + celcius + " C");
            System.out.println("" + fahrenheit + " F");
            System.out.println("" + kelvin + " K");
            break;
            case "c": System.out.println("Enter the Celcius temperature: ");
            celcius=scan.nextDouble();
            fahrenheit = (celcius*9)/5 + 32;
            kelvin = celcius + 273.15;
            System.out.println("" + celcius + " C");
            System.out.println("" + fahrenheit + " F");
            System.out.println("" + kelvin + " K");
            break;
            case "k": System.out.println("Enter the Kelvin temperature: ");
            kelvin=scan.nextDouble();
            fahrenheit = 1.8*(kelvin - 273.15) + 32;
            celcius = kelvin - 273.15;
            System.out.println("" + celcius + " C");
            System.out.println("" + fahrenheit + " F");
            System.out.println("" + kelvin + " K");
            break;
            default: System.out.println("You enter invalid choice, please choose again");
        }
        scan.close();
    }
}

这对你有帮助。
添加一个 while 循环并读取用户在 default 中的输入,它将起作用
。 当用户输入 q he/she 将退出程序。

class Test {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        double fahrenheit,celcius,kelvin;
        // changed
        System.out.println("Choose type of temperature:\nf. Fahrenheit\nc. Celcius\nk. Kelvin\nq. Quit");
        String word = scan.next();
        
        //added
        while(!word.equals("q")) {
            
            switch(word){
                case "f": {
                    System.out.println("Enter  Fahrenheit temperature: ");
                    fahrenheit=scan.nextDouble();
                    celcius = (fahrenheit-32) * 5/9;
                    kelvin = (fahrenheit + 459.67) * 5/9;
                    System.out.println("" + celcius + " C");
                    System.out.println("" + fahrenheit + " F");
                    System.out.println("" + kelvin + " K");
                    break;
                }
                case "c":{
                    System.out.println("Enter the Celcius temperature: ");
                    celcius=scan.nextDouble();
                    fahrenheit = (celcius*9)/5 + 32;
                    kelvin = celcius + 273.15;
                    System.out.println("" + celcius + " C");
                    System.out.println("" + fahrenheit + " F");
                    System.out.println("" + kelvin + " K");
                    break;
                }
                case "k": {
                    System.out.println("Enter the Kelvin temperature: ");
                    kelvin=scan.nextDouble();
                    fahrenheit = 1.8*(kelvin - 273.15) + 32;
                    celcius = kelvin - 273.15;
                    System.out.println("" + celcius + " C");
                    System.out.println("" + fahrenheit + " F");
                    System.out.println("" + kelvin + " K");
                    break;
                }
                default: {
                    System.out.println("You enter invalid choice, please choose again");
                    // added
                    System.out.println("Choose type of temperature:\nf. Fahrenheit\nc. Celcius\nk. Kelvin\nq.Quit");
                    word = scan.next();
                    break;
                    }
                }// end of swtich
            // checking for exit
            if(!word.equals("q")) {
                System.out.println("Choose type of temperature:\nf. Fahrenheit\nc. Celcius\nk. Kelvin\nq. Quit");
                word = scan.next();
            }
        }// end of while
        System.out.println("Exited");
            }
    }

输出:

Choose type of temperature:
f. Fahrenheit
c. Celcius
k. Kelvin
q. Quit
f
Enter  Fahrenheit temperature: 
45
7.222222222222222 C
45.0 F
280.3722222222222 K
Choose type of temperature:
f. Fahrenheit
c. Celcius
k. Kelvin
q. Quit
c
Enter the Celcius temperature: 
45
45.0 C
113.0 F
318.15 K
Choose type of temperature:
f. Fahrenheit
c. Celcius
k. Kelvin
q. Quit
k
Enter the Kelvin temperature: 
45
-228.14999999999998 C
-378.66999999999996 F
45.0 K
Choose type of temperature:
f. Fahrenheit
c. Celcius
k. Kelvin
q. Quit
cdvd
You enter invalid choice, please choose again
Choose type of temperature:
f. Fahrenheit
c. Celcius
k. Kelvin
q.Quit
q
Exited