国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

java計算器基于工廠模式和功能單一模式

2019-11-14 15:15:47
字體:
來源:轉載
供稿:網友
  1 import java.util.Scanner;  2   3 public class CaculationTest {  4   5     public static void main(String[] args) {  6   7         Scanner reader = new Scanner(System.in);  8         double a, b, result = 0;  9         String Operator; 10         Operation operation = null; 11  12         System.out.); 13         System.out.println("*   學號:1308060310    *"); 14         System.out.println("*   班級:網絡131班      *"); 15         System.out.println("*   姓名:王朝遠         *"); 16         System.out.println("************************"); 17  18         TwoFromConsole twoFromConsole = new TwoFromConsole(); 19         a = twoFromConsole.getFirstDoubleFromConsole(); // 獲取第一個數 20         b = twoFromConsole.getTwoDoubleFromConsole(); // 獲取第二個數 21  22         OperatorFromConsole operatorFromConsole = new OperatorFromConsole(); 23         operator = operatorFromConsole.getOperator(); // 獲取運算符號 24         do { 25             if (operator.equals("/") && b == 0) { 26                 System.out.print("除法運算分母不能為0,請重新輸入,"); 27                 b = twoFromConsole.getTwoDoubleFromConsole(); // 獲取第二個數 28                 continue; 29             } 30             break; 31         } while (true); 32  33         // 獲取要運算的對象 34         operation = Factory.getInstance(operator); 35         result = operation.getResult(a, b); 36  37         // 判斷用戶是否繼續對數運算,如果是繼續對數運算,結果的輸出方式就不一樣,并且讓用戶選擇是否再次計算 38         if (operator.equals("log")) { 39  40             System.out.println("log" + "(" + b + ")" + a + "=" + result); 41         } else { 42             System.out.println(a + operator + b + "=" + result); 43         } 44     } 45 } 46  47 class TwoFromConsole { 48  49     Scanner reader = new Scanner(System.in); 50  51     // 獲取數字的方法的具體實現 52     public double getFirstDoubleFromConsole() { 53  54         double x = 0; 55         System.out.print("請輸入第一個數字:"); 56         do { 57             double temp = 0; 58             try { 59                 temp = reader.nextDouble(); 60             } catch (Exception e) { 61                 System.out.print("請重新輸入第一個數字:"); 62                 continue; 63             } 64             x = temp; 65             break; 66         } while (true); 67         return x; 68     } 69  70     public double getTwoDoubleFromConsole() { 71  72         double x = 0; 73         System.out.print("請輸入第二個數字:"); 74         do { 75             double temp = 0; 76             try { 77                 temp = reader.nextDouble(); 78             } catch (Exception e) { 79                 System.out.print("請重新輸入第二個數字:"); 80                 continue; 81             } 82             x = temp; 83             break; 84         } while (true); 85         return x; 86     } 87 } 88  89 /** 90  * 獲取運算符類 91  */ 92 class OperatorFromConsole { 93  94     Scanner reader = new Scanner(System.in); 95  96     /** 97      * @return 合理的運算符 98      */ 99     public String getOperator() {100         System.out.print("請輸入運算符:");101 102         String operator;103         boolean b;104         do {105             operator = reader.nextLine();106             b = !(operator.equals("+") || operator.equals("-") || operator.equals("*") || operator.equals("/")107                     || operator.equals("log"));108             if (b == true) {109                 System.out.print("請重新輸入運算符:");110             }111         } while (b);112 113         return operator;114     }115 }116 117 /**118  * 功能:各個運算的父接口,子類必須實現父接口里面的方法119  */120 interface Operation {121 122     double getResult(double x, double y);123 }124 125 /**126  * 實現加法運算的類127  */128 class Add implements Operation {129 130     /**131      * 重寫接口里面的方法,并實現加法功能132      */133     @Override134     public double getResult(double x, double y) {135         // TODO Auto-generated method stub136         return x + y;137     }138 139 }140 141 /**142  * 實現減法運算的類143  */144 class Sub implements Operation {145 146     /**147      * 重寫接口里面的方法,并實現減法功能148      */149     @Override150     public double getResult(double x, double y) {151         // TODO Auto-generated method stub152         return x - y;153     }154 }155 156 /**157  * 實現乘法運算的類158  */159 class Mul implements Operation {160 161     /**162      * 重寫接口里面的方法,并實現乘法功能163      */164     @Override165     public double getResult(double x, double y) {166         // TODO Auto-generated method stub167         return x * y;168     }169 170 }171 172 /**173  * 實現除法運算的類174  */175 class Div implements Operation {176 177     /**178      * 重寫接口里面的方法,并實現除法功能179      */180     @Override181     public double getResult(double x, double y) {182         // TODO Auto-generated method stub183         return x / y;184     }185 186 }187 188 /**189  * 實現對數運算的類190  */191 class Logarithm implements Operation {192 193     /**194      * 重寫接口里面的方法,并實現對數運算功能195      */196     @Override197     public double getResult(double x, double y) {198         // TODO Auto-generated method stub199         return Math.log(x) / Math.log(y); // x表示對數,y表示底數200     }201 202 }203 204 /**205  * 生成用戶所需要的對象工廠類206  */207 class Factory {208 209     /**210      * @param operator211      *            用戶選擇的運算212      * @return 用戶所需要的對象213      */214     public static Operation getInstance(String operator) {215         Operation operation = null;216         switch (operator) {217         case "+":218             operation = new Add(); // 實例化加法對象219             break;220         case "-":221             operation = new Sub(); // 實例化減法對象222             break;223         case "*":224             operation = new Mul(); // 實例化乘法對象225             break;226         case "/":227             operation = new Div(); // 實例化除法對象228             break;229         case "log":230             operation = new Logarithm(); // 實例化對數運算對象231             break;232         }233 234         return operation;235     }236 }

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 庆元县| 怀来县| 察隅县| 青州市| 库车县| 桃江县| 驻马店市| 栖霞市| 德令哈市| 常熟市| 织金县| 东丰县| 吉安市| 鹿邑县| 东明县| 武隆县| 若羌县| 海兴县| 石嘴山市| 尚义县| 靖江市| 册亨县| 洪洞县| 南部县| 扬州市| 太湖县| 钟祥市| 疏附县| 民勤县| 德安县| 武隆县| 金堂县| 全椒县| 武平县| 五寨县| 松滋市| 晴隆县| 都江堰市| 洛宁县| 井冈山市| 双桥区|