Java中switch语句的默认选项无法正常工作。

huangapple go评论124阅读模式
英文:

Java - default option does not fuction in switch

问题

import java.util.Scanner;

public class Temperature {
    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. Celsius\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 Celsius 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 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 entered an invalid choice, please choose again");
        }
        scan.close();
    }
}
英文:

hello I wrote a program to convert one temperature from one to another using switch.

the default does not fuction:

When i enter a valid option it works perfectly fine, but when i choose invalid choice it goes the default outputting the lines: You enter invalid choice, please choose again and stop workings without allowing me to choose another choice.

earlier when I tested it, the default allowed me to choose another option.

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();
}
}

答案1

得分: 2

这可能会对你有所帮助。<br>
添加一个while循环并在default中读取用户输入,它将正常工作<br>。
当用户输入q时,他/她将退出程序。

class Test {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        double fahrenheit, celcius, kelvin;
        // 更改了
        System.out.println("选择温度类型:\nf. 华氏度\nc. 摄氏度\nk. 开尔文\nq. 退出");
        String word = scan.next();

        // 添加了
        while (!word.equals("q")) {

            switch (word) {
                case "f": {
                    System.out.println("输入华氏度温度:");
                    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("输入摄氏度温度:");
                    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("输入开尔文温度:");
                    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("您输入了无效的选项,请重新选择");
                    // 添加了
                    System.out.println("选择温度类型:\nf. 华氏度\nc. 摄氏度\nk. 开尔文\nq.退出");
                    word = scan.next();
                    break;
                }
            } // 结束 switch
            // 检查是否退出
            if (!word.equals("q")) {
                System.out.println("选择温度类型:\nf. 华氏度\nc. 摄氏度\nk. 开尔文\nq. 退出");
                word = scan.next();
            }
        } // 结束 while
        System.out.println("已退出");
    }
}

输出:

选择温度类型:
f. 华氏度
c. 摄氏度
k. 开尔文
q. 退出
f
输入华氏度温度:
45
7.222222222222222 C
45.0 F
280.3722222222222 K
选择温度类型:
f. 华氏度
c. 摄氏度
k. 开尔文
q. 退出
c
输入摄氏度温度:
45
45.0 C
113.0 F
318.15 K
选择温度类型:
f. 华氏度
c. 摄氏度
k. 开尔文
q. 退出
k
输入开尔文温度:
45
-228.14999999999998 C
-378.66999999999996 F
45.0 K
选择温度类型:
f. 华氏度
c. 摄氏度
k. 开尔文
q. 退出
cdvd
您输入了无效的选项,请重新选择
选择温度类型:
f. 华氏度
c. 摄氏度
k. 开尔文
q.退出
q
已退出
英文:

This could help you.<br>
add a while loop and read the input of user in default and it will work<br>.
When user puts q he/she will exit the program.

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

Output:

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

huangapple
  • 本文由 发表于 2020年10月3日 15:56:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/64181972.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定