1. 程式人生 > >設計模式-工廠模式探索(一)加減乘除程式例項

設計模式-工廠模式探索(一)加減乘除程式例項

package com.duanshiyi.operation;

import java.util.Scanner;

public class Count {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        /**
         * 實現加減乘除小程式
         */
        try{
            System.out.println("請輸入一個數字:");
            Scanner sc = new Scanner(System.in);
            int numberA = sc.nextInt();
            System.out.println("請輸入想要進行的運算操作(包括+、 -、  *、  /):");
            String op = sc.next();
            System.out.println("請再輸入一個數字:");
            int numberB = sc.nextInt();
            sc.close();
            int result = 0 ;
            if(op.equals("+"))
                result = numberA + numberB;
            if(op.equals("-"))
                result = numberA - numberB;
            if(op.equals("*"))
                result = numberA * numberB;
            if(op.equals("/"))
                result = numberA / numberB;
            System.out.println("result : " + result);
        }catch(Exception e){
            System.out.println("除數不能為零,異常資訊如下:");
            e.printStackTrace();
            
        }

    }

}