16_簡單計算器實現

神莹發表於2024-07-01

02_簡單計算器實現

public class Demo {
    public static void main(String[] args) {
        int flag = 0;
            while(flag != 5){
                System.out.println("選擇加法請按1,2,3,4"+"\t"+"退出請按5");
                Scanner scanner = new Scanner(System.in);
                int c = scanner.nextInt();
                switch(c) {
                    case 1:
                        System.out.println("請輸入第一個數a:");
                        Scanner scanner1 = new Scanner(System.in);
                        double a = scanner1.nextDouble();
                        System.out.println("請輸入第2個數b:");
                        Scanner scanner2 = new Scanner(System.in);
                        double b = scanner2.nextDouble();
                        Demo demo = new Demo();
                        double sum = Demo.add(a, b);
                        System.out.println("結果為" + sum);
//                        scanner1.close();
//                        scanner2.close();
                        break;
                    case 5:
                        flag = 5;
                        break;
                }
//                scanner.close();
            }
        }

    public static double add(double a, double b) {
        return a + b;
    }
    public static double sub(double a, double b) {
        return a - b;
    }
    public static double mul(double a, double b) {
        return a * b;
    }
    public static double div(double a, double b) {
        return a / b;
    }
}

相關文章