在Java中优化温度转换器

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

Optimization of Temperature converter in Java

问题

  1. public static void main(String[] args) {
  2. Double temperature, result;
  3. String menuChoice = "1. Celsius to Fahrenheit \n2. Fahrenheit to Celsius";
  4. int anyChoiceMenu = 3;
  5. System.out.println("Choose a conversion");
  6. System.out.println(menuChoice);
  7. Scanner sc = new Scanner(System.in);
  8. while (true) {
  9. try {
  10. anyChoiceMenu = sc.nextInt();
  11. System.out.println("What temperature you wanna convert: ");
  12. temperature = sc.nextDouble();
  13. if (anyChoiceMenu == 1) {
  14. result = 1.8 * temperature + 32;
  15. System.out.println("Result: " + result + " Fahrenheit");
  16. break;
  17. } else if (anyChoiceMenu == 2) {
  18. result = (5 * (temperature - 32.0)) / 9.0;
  19. System.out.println("Result: " + result + " Celsius");
  20. break;
  21. } else {
  22. System.out.println("Wrong number. Try again!");
  23. sc.nextLine();
  24. }
  25. } catch (Exception e) {
  26. System.out.println("Only numbers bro.");
  27. System.out.println(menuChoice);
  28. sc.nextLine();
  29. continue;
  30. }
  31. }
  32. }
英文:

So I am struggling to get back to programming and I made this piece of code that converts Celsius to Fahrenheit and vice versa.

Its working fine but I must make it shorter. Can you make it work with less lines?

  1. public static void main(String[] args) {
  2. Double temperature = 0.0;
  3. Double result = 0.0;
  4. String menuChoise = "1. Celsius to Fahrenheit \n2. Fahrenheit to Celsius";
  5. int anyChoiseMenu = 3;
  6. System.out.println("Chose a conversion");
  7. System.out.println(menuChoise);
  8. Scanner sc = new Scanner(System.in);
  9. while (true) {
  10. try {
  11. anyChoiseMenu = sc.nextInt();
  12. if (anyChoiseMenu == 1) {
  13. System.out.println("What temperature you wanna convert: ");
  14. temperature = sc.nextDouble();
  15. result = (1.8) * temperature + 32;
  16. System.out.println("Result: " + result + " Fahrenheit");
  17. break;
  18. } else if (anyChoiseMenu == 2) {
  19. System.out.println("What temperature you wanna convert: ");
  20. temperature = sc.nextDouble();
  21. result = ((5 * (temperature - 32.0)) / 9.0);
  22. System.out.println("Result: " + result + " Celsius");
  23. break;
  24. } else {
  25. System.out.println("Wrong number. Try again!");
  26. sc.nextLine();
  27. }
  28. } catch (Exception e) {
  29. System.out.println("Only numbers bro.");
  30. System.out.println(menuChoise);
  31. sc.nextLine();
  32. continue;
  33. }
  34. }
  35. }

答案1

得分: 1

你的代码似乎运行得很好。通过以下一些技巧,你可以减少一些代码行数。

  1. import java.util.InputMismatchException;
  2. import java.util.Scanner;
  3. class Main {
  4. public static void main(String[] args) {
  5. Scanner sc = new Scanner(System.in);
  6. String menuChoice = "\n选择转换:\n1. 摄氏度到华氏度\n2. 华氏度到摄氏度";
  7. int menu;
  8. while (true) {
  9. System.out.println(menuChoice);
  10. try {
  11. menu = sc.nextInt();
  12. } catch (InputMismatchException e) {
  13. System.out.println("无效输入,只允许输入数字!");
  14. sc.nextLine();
  15. continue;
  16. }
  17. if (menu >= 3) {
  18. System.out.println("错误的数字。请重试!");
  19. } else {
  20. System.out.print("你想要转换的温度:");
  21. double temperature = sc.nextDouble();
  22. if (menu == 1) {
  23. temperature = 1.8 * temperature + 32;
  24. } else if (menu == 2) {
  25. temperature = (temperature - 32) / 1.8;
  26. }
  27. String grade = (menu == 1) ? "摄氏度" : "华氏度";
  28. System.out.format("结果:%.2f " + grade + '\n', temperature);
  29. break;
  30. }
  31. }
  32. }
  33. }
英文:

Your code seems to be running just fine. With a few tricks, as seen below you could cut a few lines of code.

  1. import java.util.InputMismatchException;
  2. import java.util.Scanner;
  3. class Main {
  4. public static void main(String[] args) {
  5. Scanner sc = new Scanner(System.in);
  6. String menuChoice = "\nChoose a conversion: \n1. Celsius to Fahrenheit \n2. Fahrenheit to Celsius";
  7. int menu;
  8. while (true) {
  9. System.out.println(menuChoice);
  10. try {
  11. menu = sc.nextInt();
  12. } catch (InputMismatchException e){
  13. System.out.println("Invalid input, only numbers are allowed!");
  14. sc.nextLine();
  15. continue;
  16. }
  17. if (menu >= 3) {
  18. System.out.println("Wrong number. Try again!");
  19. } else {
  20. System.out.print("What temperature you wanna convert: ");
  21. double temperature = sc.nextDouble();
  22. if (menu == 1) {
  23. temperature = 1.8 * temperature + 32;
  24. } else if (menu == 2) {
  25. temperature = (temperature - 32) / 1.8;
  26. }
  27. String grade = (menu == 1)? "Celsius": "Fahrenheit";
  28. System.out.format("Result: %.2f " + grade + '\n', temperature);
  29. break;
  30. }
  31. }
  32. }
  33. }

答案2

得分: 0

  1. public static void main(String... args) {
  2. Scanner scan = new Scanner(System.in);
  3. scan.useLocale(Locale.ENGLISH);
  4. while (true) {
  5. System.out.println("选择转换方式:");
  6. System.out.println("1. 摄氏度转华氏度");
  7. System.out.println("2. 华氏度转摄氏度");
  8. System.out.println("3. 退出");
  9. System.out.print("> ");
  10. int menu = scan.nextInt();
  11. if (menu == 1) {
  12. System.out.print("请输入要转换的温度:");
  13. double celsius = scan.nextDouble();
  14. double fahrenheit = 1.8 * celsius + 32;
  15. System.out.format(Locale.ENGLISH, "结果:%.2f 华氏度\n", fahrenheit);
  16. } else if (menu == 2) {
  17. System.out.println("请输入要转换的温度:");
  18. double fahrenheit = scan.nextDouble();
  19. double celsius = (fahrenheit - 32) / 1.8;
  20. System.out.format(Locale.ENGLISH, "结果:%.2f 摄氏度\n", celsius);
  21. } else if (menu == 3)
  22. break;
  23. else
  24. System.out.println("错误数字。请重试!");
  25. System.out.println();
  26. }
  27. }
英文:
  1. public static void main(String... args) {
  2. Scanner scan = new Scanner(System.in);
  3. scan.useLocale(Locale.ENGLISH);
  4. while (true) {
  5. System.out.println("Chose a conversion:");
  6. System.out.println("1. Celsius to Fahrenheit");
  7. System.out.println("2. Fahrenheit to Celsius");
  8. System.out.println("3. Exit");
  9. System.out.print("> ");
  10. int menu = scan.nextInt();
  11. if (menu == 1) {
  12. System.out.print("What temperature you wanna convert: ");
  13. double celsius = scan.nextDouble();
  14. double fahrenheit = 1.8 * celsius + 32;
  15. System.out.format(Locale.ENGLISH, "Result: %.2f Fahrenheit\n", fahrenheit);
  16. } else if (menu == 2) {
  17. System.out.println("What temperature you wanna convert: ");
  18. double fahrenheit = scan.nextDouble();
  19. double celsius = (fahrenheit - 32) / 1.8;
  20. System.out.format(Locale.ENGLISH, "Result: %.2f Celsius\n", celsius);
  21. } else if (menu == 3)
  22. break;
  23. else
  24. System.out.println("Wrong number. Try again!");
  25. System.out.println();
  26. }
  27. }

huangapple
  • 本文由 发表于 2020年9月22日 03:15:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/63998630.html
匿名

发表评论

匿名网友

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

确定