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

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

Java - default option does not fuction in switch

问题

  1. import java.util.Scanner;
  2. public class Temperature {
  3. public static void main(String[] args){
  4. Scanner scan = new Scanner(System.in);
  5. double fahrenheit, celcius, kelvin;
  6. System.out.println("Choose type of temperature:\nf. Fahrenheit\nc. Celsius\nk. Kelvin");
  7. String word = scan.nextLine();
  8. switch(word){
  9. case "f":
  10. System.out.println("Enter Fahrenheit temperature: ");
  11. fahrenheit = scan.nextDouble();
  12. celcius = (fahrenheit - 32) * 5/9;
  13. kelvin = (fahrenheit + 459.67) * 5/9;
  14. System.out.println("" + celcius + " °C");
  15. System.out.println("" + fahrenheit + " °F");
  16. System.out.println("" + kelvin + " K");
  17. break;
  18. case "c":
  19. System.out.println("Enter Celsius temperature: ");
  20. celcius = scan.nextDouble();
  21. fahrenheit = (celcius * 9) / 5 + 32;
  22. kelvin = celcius + 273.15;
  23. System.out.println("" + celcius + " °C");
  24. System.out.println("" + fahrenheit + " °F");
  25. System.out.println("" + kelvin + " K");
  26. break;
  27. case "k":
  28. System.out.println("Enter Kelvin temperature: ");
  29. kelvin = scan.nextDouble();
  30. fahrenheit = 1.8 * (kelvin - 273.15) + 32;
  31. celcius = kelvin - 273.15;
  32. System.out.println("" + celcius + " °C");
  33. System.out.println("" + fahrenheit + " °F");
  34. System.out.println("" + kelvin + " K");
  35. break;
  36. default:
  37. System.out.println("You entered an invalid choice, please choose again");
  38. }
  39. scan.close();
  40. }
  41. }
英文:

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.

  1. import java.util.Scanner;
  2. public class Tempature {
  3. public static void main(String[] args){
  4. Scanner scan = new Scanner(System.in);
  5. double fahrenheit,celcius,kelvin;
  6. System.out.println("Choose type of temperature:\nf. Fahrenheit\nc. Celcius\nk. Kelvin");
  7. String word = scan.nextLine();
  8. switch(word){
  9. case "f": System.out.println("Enter Fahrenheit temperature: ");
  10. fahrenheit=scan.nextDouble();
  11. celcius = (fahrenheit-32) * 5/9;
  12. kelvin = (fahrenheit + 459.67) * 5/9;
  13. System.out.println("" + celcius + " C");
  14. System.out.println("" + fahrenheit + " F");
  15. System.out.println("" + kelvin + " K");
  16. break;
  17. case "c": System.out.println("Enter the Celcius temperature: ");
  18. celcius=scan.nextDouble();
  19. fahrenheit = (celcius*9)/5 + 32;
  20. kelvin = celcius + 273.15;
  21. System.out.println("" + celcius + " C");
  22. System.out.println("" + fahrenheit + " F");
  23. System.out.println("" + kelvin + " K");
  24. break;
  25. case "k": System.out.println("Enter the Kelvin temperature: ");
  26. kelvin=scan.nextDouble();
  27. fahrenheit = 1.8*(kelvin - 273.15) + 32;
  28. celcius = kelvin - 273.15;
  29. System.out.println("" + celcius + " C");
  30. System.out.println("" + fahrenheit + " F");
  31. System.out.println("" + kelvin + " K");
  32. break;
  33. default: System.out.println("You enter invalid choice, please choose again");
  34. }
  35. scan.close();
  36. }
  37. }

答案1

得分: 2

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

  1. class Test {
  2. public static void main(String[] args) {
  3. Scanner scan = new Scanner(System.in);
  4. double fahrenheit, celcius, kelvin;
  5. // 更改了
  6. System.out.println("选择温度类型:\nf. 华氏度\nc. 摄氏度\nk. 开尔文\nq. 退出");
  7. String word = scan.next();
  8. // 添加了
  9. while (!word.equals("q")) {
  10. switch (word) {
  11. case "f": {
  12. System.out.println("输入华氏度温度:");
  13. fahrenheit = scan.nextDouble();
  14. celcius = (fahrenheit - 32) * 5 / 9;
  15. kelvin = (fahrenheit + 459.67) * 5 / 9;
  16. System.out.println("" + celcius + " C");
  17. System.out.println("" + fahrenheit + " F");
  18. System.out.println("" + kelvin + " K");
  19. break;
  20. }
  21. case "c": {
  22. System.out.println("输入摄氏度温度:");
  23. celcius = scan.nextDouble();
  24. fahrenheit = (celcius * 9) / 5 + 32;
  25. kelvin = celcius + 273.15;
  26. System.out.println("" + celcius + " C");
  27. System.out.println("" + fahrenheit + " F");
  28. System.out.println("" + kelvin + " K");
  29. break;
  30. }
  31. case "k": {
  32. System.out.println("输入开尔文温度:");
  33. kelvin = scan.nextDouble();
  34. fahrenheit = 1.8 * (kelvin - 273.15) + 32;
  35. celcius = kelvin - 273.15;
  36. System.out.println("" + celcius + " C");
  37. System.out.println("" + fahrenheit + " F");
  38. System.out.println("" + kelvin + " K");
  39. break;
  40. }
  41. default: {
  42. System.out.println("您输入了无效的选项,请重新选择");
  43. // 添加了
  44. System.out.println("选择温度类型:\nf. 华氏度\nc. 摄氏度\nk. 开尔文\nq.退出");
  45. word = scan.next();
  46. break;
  47. }
  48. } // 结束 switch
  49. // 检查是否退出
  50. if (!word.equals("q")) {
  51. System.out.println("选择温度类型:\nf. 华氏度\nc. 摄氏度\nk. 开尔文\nq. 退出");
  52. word = scan.next();
  53. }
  54. } // 结束 while
  55. System.out.println("已退出");
  56. }
  57. }

输出:

  1. 选择温度类型:
  2. f. 华氏度
  3. c. 摄氏度
  4. k. 开尔文
  5. q. 退出
  6. f
  7. 输入华氏度温度:
  8. 45
  9. 7.222222222222222 C
  10. 45.0 F
  11. 280.3722222222222 K
  12. 选择温度类型:
  13. f. 华氏度
  14. c. 摄氏度
  15. k. 开尔文
  16. q. 退出
  17. c
  18. 输入摄氏度温度:
  19. 45
  20. 45.0 C
  21. 113.0 F
  22. 318.15 K
  23. 选择温度类型:
  24. f. 华氏度
  25. c. 摄氏度
  26. k. 开尔文
  27. q. 退出
  28. k
  29. 输入开尔文温度:
  30. 45
  31. -228.14999999999998 C
  32. -378.66999999999996 F
  33. 45.0 K
  34. 选择温度类型:
  35. f. 华氏度
  36. c. 摄氏度
  37. k. 开尔文
  38. q. 退出
  39. cdvd
  40. 您输入了无效的选项,请重新选择
  41. 选择温度类型:
  42. f. 华氏度
  43. c. 摄氏度
  44. k. 开尔文
  45. q.退出
  46. q
  47. 已退出
英文:

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.

  1. class Test {
  2. public static void main(String[] args) {
  3. Scanner scan = new Scanner(System.in);
  4. double fahrenheit,celcius,kelvin;
  5. // changed
  6. System.out.println(&quot;Choose type of temperature:\nf. Fahrenheit\nc. Celcius\nk. Kelvin\nq. Quit&quot;);
  7. String word = scan.next();
  8. //added
  9. while(!word.equals(&quot;q&quot;)) {
  10. switch(word){
  11. case &quot;f&quot;: {
  12. System.out.println(&quot;Enter Fahrenheit temperature: &quot;);
  13. fahrenheit=scan.nextDouble();
  14. celcius = (fahrenheit-32) * 5/9;
  15. kelvin = (fahrenheit + 459.67) * 5/9;
  16. System.out.println(&quot;&quot; + celcius + &quot; C&quot;);
  17. System.out.println(&quot;&quot; + fahrenheit + &quot; F&quot;);
  18. System.out.println(&quot;&quot; + kelvin + &quot; K&quot;);
  19. break;
  20. }
  21. case &quot;c&quot;:{
  22. System.out.println(&quot;Enter the Celcius temperature: &quot;);
  23. celcius=scan.nextDouble();
  24. fahrenheit = (celcius*9)/5 + 32;
  25. kelvin = celcius + 273.15;
  26. System.out.println(&quot;&quot; + celcius + &quot; C&quot;);
  27. System.out.println(&quot;&quot; + fahrenheit + &quot; F&quot;);
  28. System.out.println(&quot;&quot; + kelvin + &quot; K&quot;);
  29. break;
  30. }
  31. case &quot;k&quot;: {
  32. System.out.println(&quot;Enter the Kelvin temperature: &quot;);
  33. kelvin=scan.nextDouble();
  34. fahrenheit = 1.8*(kelvin - 273.15) + 32;
  35. celcius = kelvin - 273.15;
  36. System.out.println(&quot;&quot; + celcius + &quot; C&quot;);
  37. System.out.println(&quot;&quot; + fahrenheit + &quot; F&quot;);
  38. System.out.println(&quot;&quot; + kelvin + &quot; K&quot;);
  39. break;
  40. }
  41. default: {
  42. System.out.println(&quot;You enter invalid choice, please choose again&quot;);
  43. // added
  44. System.out.println(&quot;Choose type of temperature:\nf. Fahrenheit\nc. Celcius\nk. Kelvin\nq.Quit&quot;);
  45. word = scan.next();
  46. break;
  47. }
  48. }// end of swtich
  49. // checking for exit
  50. if(!word.equals(&quot;q&quot;)) {
  51. System.out.println(&quot;Choose type of temperature:\nf. Fahrenheit\nc. Celcius\nk. Kelvin\nq. Quit&quot;);
  52. word = scan.next();
  53. }
  54. }// end of while
  55. System.out.println(&quot;Exited&quot;);
  56. }
  57. }

Output:

  1. Choose type of temperature:
  2. f. Fahrenheit
  3. c. Celcius
  4. k. Kelvin
  5. q. Quit
  6. f
  7. Enter Fahrenheit temperature:
  8. 45
  9. 7.222222222222222 C
  10. 45.0 F
  11. 280.3722222222222 K
  12. Choose type of temperature:
  13. f. Fahrenheit
  14. c. Celcius
  15. k. Kelvin
  16. q. Quit
  17. c
  18. Enter the Celcius temperature:
  19. 45
  20. 45.0 C
  21. 113.0 F
  22. 318.15 K
  23. Choose type of temperature:
  24. f. Fahrenheit
  25. c. Celcius
  26. k. Kelvin
  27. q. Quit
  28. k
  29. Enter the Kelvin temperature:
  30. 45
  31. -228.14999999999998 C
  32. -378.66999999999996 F
  33. 45.0 K
  34. Choose type of temperature:
  35. f. Fahrenheit
  36. c. Celcius
  37. k. Kelvin
  38. q. Quit
  39. cdvd
  40. You enter invalid choice, please choose again
  41. Choose type of temperature:
  42. f. Fahrenheit
  43. c. Celcius
  44. k. Kelvin
  45. q.Quit
  46. q
  47. 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:

确定