Java利用Scanner 加Swich實現計算器

1.229km/s發表於2020-11-29

Java利用Scanner 加Swich實現計算器

package base;

import java.util.Scanner;

public class Demo1 {
    public static void main(String[] args) {
        //System.out.println(f(5));
        Scanner a=new Scanner(System.in);
        double sum = 0;
        System.out.println("請輸入第一個數字:");
        double num1=a.nextDouble();
        System.out.println("請輸入第二個數字:");
        double num2=a.nextDouble();
        System.out.println("請輸入運算子:");
        String op=a.next();
        switch (op){
            case "+":
                sum=num1+num2;
                System.out.println(num1+"+"+num2+"="+sum);
                break;
            case "-":
                sum=num1-num2;
                System.out.println(num1+"-"+num2+"="+sum);
                break;
            case "*":
                sum=num1*num2;
                System.out.println(num1+"*"+num2+"="+sum);
                break;
            case "/":
                sum=num1/num2;
                System.out.println(num1+"/"+num2+"="+sum);
                break;
        }

    }
    //階乘問題
   /* public static int f(int n){
        if(n==1){
            return 1;
        }
        return n*(f(n-1));
    }*/

}

xinyue Day2

相關文章