1. 程式人生 > 實用技巧 >一個簡單的計算器小程式

一個簡單的計算器小程式

一個簡單的計算器小程式

package com.zjl.method;

import java.util.Scanner;

public class Demo07 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("請輸入第一個數");
        double a = scanner.nextDouble();
        System.out.println("請輸入+,-,*,/任意一個運算子");
        String symbol = scanner.next();
        System.out.println("請輸入第二個數");
        double b = scanner.nextDouble();
            switch (symbol) {
                case "+":
                    System.out.println("結果為:"+add(a, b));
                    break;
                case "-":
                    System.out.println("結果為:"+sub(a, b));
                    break;
                case "*":
                    System.out.println("結果為:"+mul(a, b));
                    break;
                case "/":
                    System.out.println("結果為:"+div(a, b));
                    break;
            }

    }
    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;
    }

}