1. 程式人生 > 其它 >簡單計算器的實現

簡單計算器的實現

計算器實現

package com.snowMoon;

import java.util.Scanner;

public class Calculator {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("輸入你要進行的運算方法");
        System.out.println("1.加法\t2.減法\t3.乘法\t4.除法");
        int i = scanner.nextInt();
        if (i<=4&&i!=0){
            System.out.println("輸入你需要進行運算的數");
            double a = scanner.nextDouble();
            double b = scanner.nextDouble();

            switch (i) {
                case 1:
                    System.out.println("結果為" + (a + b));
                    break;
                case 2:
                    System.out.println("結果為" + (a - b));
                    break;
                case 3:
                    System.out.println("結果為" + (a * b));
                    break;
                case 4:
                    System.out.println("結果為" + (a / b));
                    break;
            }
        }else{
            System.out.println("不合法的輸入");
        }
    }
}