在Java中优化温度转换器

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

Optimization of Temperature converter in Java

问题

public static void main(String[] args) {
    Double temperature, result;
    
    String menuChoice = "1. Celsius to Fahrenheit \n2. Fahrenheit to Celsius";
    int anyChoiceMenu = 3;

    System.out.println("Choose a conversion");
    System.out.println(menuChoice);

    Scanner sc = new Scanner(System.in);

    while (true) {
        try {
            anyChoiceMenu = sc.nextInt();
            System.out.println("What temperature you wanna convert: ");
            temperature = sc.nextDouble();
            
            if (anyChoiceMenu == 1) {
                result = 1.8 * temperature + 32;
                System.out.println("Result: " + result + " Fahrenheit");
                break;
            } else if (anyChoiceMenu == 2) {
                result = (5 * (temperature - 32.0)) / 9.0;
                System.out.println("Result: " + result + " Celsius");
                break;
            } else {
                System.out.println("Wrong number. Try again!");
                sc.nextLine();
            }
        } catch (Exception e) {
            System.out.println("Only numbers bro.");
            System.out.println(menuChoice);
            sc.nextLine();
            continue;
        }
    }
}
英文:

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?

public static void main(String[] args) {
    Double temperature = 0.0;
    Double result = 0.0;
    
    String menuChoise = "1. Celsius to Fahrenheit \n2. Fahrenheit to Celsius";
    int anyChoiseMenu = 3;

    System.out.println("Chose a conversion");
    System.out.println(menuChoise);

    Scanner sc = new Scanner(System.in);

    while (true) {
        try {
            anyChoiseMenu = sc.nextInt();
            if (anyChoiseMenu == 1) {
                System.out.println("What temperature you wanna convert: ");
                temperature = sc.nextDouble();
                result = (1.8) * temperature + 32;
                System.out.println("Result: " + result + " Fahrenheit");
                break;
            } else if (anyChoiseMenu == 2) {
                System.out.println("What temperature you wanna convert: ");
                temperature = sc.nextDouble();
                result = ((5 * (temperature - 32.0)) / 9.0);
                System.out.println("Result: " + result + " Celsius");
                break;
            } else {
                System.out.println("Wrong number. Try again!");
                sc.nextLine();
            }
        } catch (Exception e) {
            System.out.println("Only numbers bro.");
            System.out.println(menuChoise);
            sc.nextLine();
            continue;
        }
    }
}

答案1

得分: 1

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

import java.util.InputMismatchException;
import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String menuChoice = "\n选择转换:\n1. 摄氏度到华氏度\n2. 华氏度到摄氏度";
        int menu;

        while (true) {
            System.out.println(menuChoice);
            try {
                menu = sc.nextInt();
            } catch (InputMismatchException e) {
                System.out.println("无效输入,只允许输入数字!");
                sc.nextLine();
                continue;
            }

            if (menu >= 3) {
                System.out.println("错误的数字。请重试!");
            } else {
                System.out.print("你想要转换的温度:");
                double temperature = sc.nextDouble();
                if (menu == 1) {
                    temperature = 1.8 * temperature + 32;
                } else if (menu == 2) {
                    temperature = (temperature - 32) / 1.8;
                }
                String grade = (menu == 1) ? "摄氏度" : "华氏度";
                System.out.format("结果:%.2f " + grade + '\n', temperature);
                break;
            }
        }
    }
}
英文:

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

import java.util.InputMismatchException;
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String menuChoice = "\nChoose a conversion: \n1. Celsius to Fahrenheit \n2. Fahrenheit to Celsius";
int menu;
while (true) {
System.out.println(menuChoice);
try {
menu = sc.nextInt();
} catch (InputMismatchException e){
System.out.println("Invalid input, only numbers are allowed!");
sc.nextLine();
continue;
}
if (menu >= 3) {
System.out.println("Wrong number. Try again!");
} else {
System.out.print("What temperature you wanna convert: ");
double temperature = sc.nextDouble();
if (menu == 1) {
temperature = 1.8 * temperature + 32;
} else if (menu == 2) {
temperature = (temperature - 32) / 1.8;
}
String grade = (menu == 1)? "Celsius": "Fahrenheit";
System.out.format("Result: %.2f " + grade + '\n', temperature);
break;
}
}
}
}

答案2

得分: 0

public static void main(String... args) {
    Scanner scan = new Scanner(System.in);
    scan.useLocale(Locale.ENGLISH);

    while (true) {
        System.out.println("选择转换方式:");
        System.out.println("1. 摄氏度转华氏度");
        System.out.println("2. 华氏度转摄氏度");
        System.out.println("3. 退出");

        System.out.print("> ");
        int menu = scan.nextInt();

        if (menu == 1) {
            System.out.print("请输入要转换的温度:");
            double celsius = scan.nextDouble();
            double fahrenheit = 1.8 * celsius + 32;
            System.out.format(Locale.ENGLISH, "结果:%.2f 华氏度\n", fahrenheit);
        } else if (menu == 2) {
            System.out.println("请输入要转换的温度:");
            double fahrenheit = scan.nextDouble();
            double celsius = (fahrenheit - 32) / 1.8;
            System.out.format(Locale.ENGLISH, "结果:%.2f 摄氏度\n", celsius);
        } else if (menu == 3)
            break;
        else
            System.out.println("错误数字。请重试!");

        System.out.println();
    }
}
英文:
public static void main(String... args) {
Scanner scan = new Scanner(System.in);
scan.useLocale(Locale.ENGLISH);
while (true) {
System.out.println("Chose a conversion:");
System.out.println("1. Celsius to Fahrenheit");
System.out.println("2. Fahrenheit to Celsius");
System.out.println("3. Exit");
System.out.print("> ");
int menu = scan.nextInt();
if (menu == 1) {
System.out.print("What temperature you wanna convert: ");
double celsius = scan.nextDouble();
double fahrenheit = 1.8 * celsius + 32;
System.out.format(Locale.ENGLISH, "Result: %.2f Fahrenheit\n", fahrenheit);
} else if (menu == 2) {
System.out.println("What temperature you wanna convert: ");
double fahrenheit = scan.nextDouble();
double celsius = (fahrenheit - 32) / 1.8;
System.out.format(Locale.ENGLISH, "Result: %.2f Celsius\n", celsius);
} else if (menu == 3)
break;
else
System.out.println("Wrong number. Try again!");
System.out.println();
}
}

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:

确定