練習題---三角運算

          Posted on 2007-10-16 14:09 久城 閱讀(823) 評論(0)  編輯  收藏 所屬分類: JavaTest
          好久沒有做練習題了。

          編寫一個小的計算程序,用來進行三角運算(
          Sin Costan…),該程序通過交互接收用戶輸入,例如:
          系統剛啟動的時候處于提示狀態:
          Function>
          這時用戶可以輸入函數名稱,輸入sin表示想進行sin運算,此時再提醒用戶輸入角度:
          Angel>
          用戶可以輸入角度,
          計算完畢后,以Result<方式輸出結果,并且重新回到Function>的狀態下。
          在任何時候用戶輸入非法,則顯示Error<,在其后描述具體的錯誤原因。然后重新回到錯誤輸入前狀態。
          1)語言不限
          2)支持很方便的擴展
          (3)變量的命名和使用要符合學習的內容

          代碼如下:
          /**---------------------------------------------
           *   Class Name   : YW2_Test01.java
           *   Purpose      : 編寫一個小的計算程序,用來進行三角運算(Sin, Cos,tan…),該程序通過交互接收用戶輸入
           *
           *   
          @author realsmy
           *   
          @since 2007/10/16
           *
           *   Copyright realsmy. All rights reserved.
           *---------------------------------------------
           
          */

          package com.neusoft.test;

          import java.io.BufferedReader;
          import java.io.IOException;
          import java.io.InputStreamReader;

          // 三角函數名的枚舉類型
          enum FuncName{
              SIN,
              COS,
              TAN
          }


          public class YW5_Test01{
              
              
          // 三角函數名
              private FuncName function;
              
              
          // 表示角度
              private double angel;
              
              
          // 圓周率常量
              private static double PAI = 3.14159265;

              
          /**
               * ---------------------------------------------
               * Method Name : YW5_Test01 
               * Exposition : 構造函數,執行運算過程
               * ---------------------------------------------
               
          */

              
          public YW5_Test01(){
                  
          // 是指三角函數名
                  setFunction();
                  
          // 設置角度
                  setAngel();
                  
          // 計算出結果
                  getResult();
              }

              
              
          /**
               * ---------------------------------------------
               * Method Name : setFuncName 
               * Exposition : 設置三角函數名字
               * ---------------------------------------------
               
          */

              
          private void setFuncName(FuncName func) {
                  
          this.function = func;
              }

              
          /**
               * ---------------------------------------------
               * Method Name : setFunction 
               * Exposition : 設置三角函數名字
               * ---------------------------------------------
               
          */

              
          private void setFunction(){
                  System.out.print(
          "Function> ");     
                  
          if ( !checkFunction(getFunction())) {
                      System.out.println(
          "error: worng function name, please input again:");
                      setFunction();
                  }

              }

              
          /**
               * ---------------------------------------------
               * Method Name : getFunction 
               * Exposition : 取得三角函數名字
               * ---------------------------------------------
               
          */

              
          private String getFunction(){
                  String func 
          = null;
                  
          try {
                      BufferedReader in 
          = new BufferedReader(new InputStreamReader(System.in));
                      func 
          = in.readLine().toUpperCase();
                  }
           catch (IOException e) {
                  }

                  
          return func;
              }

              
          /**
               * ---------------------------------------------
               * Method Name : checkFunction 
               * Exposition : 檢查三角函數名字
               * ---------------------------------------------
               
          */

              
          private Boolean checkFunction(String func){
                  
          for ( FuncName funcName : FuncName.values()) {
                      
          if( funcName.toString().equals(func)) {
                          setFuncName(funcName);
                          
          return true;
                      }

                  }

                  
          return false;
              }

              
          /**
               * ---------------------------------------------
               * Method Name : setAngel 
               * Exposition : 設置角度
               * ---------------------------------------------
               
          */

              
          private void setAngel(){
                  System.out.print(
          "Angel> ");     
                  getAngel();
              }

              
          /**
               * ---------------------------------------------
               * Method Name : getAngel 
               * Exposition : 取得角度
               * ---------------------------------------------
               
          */

              
          private double getAngel(){
                  
          try {
                      BufferedReader in 
          = new BufferedReader(new InputStreamReader(System.in));
                      angel 
          = Double.parseDouble(in.readLine());
                  }
           catch(NumberFormatException ne){
                      System.out.println(
          "The input is not a number, please input again:");
                      setAngel();
                  }
           catch (IOException e) {
                  }

                  
          return angel;
              }

              
          /**
               * ---------------------------------------------
               * Method Name : getResult 
               * Exposition : 取得結果
               * ---------------------------------------------
               
          */

              
          private void getResult(){
                  
          double result = 0;
                  
          switch (function){
                  
          case SIN:
                      result 
          = Math.sin(angel*PAI/180);
                      
          break;
                  
          case COS:
                      result 
          = Math.cos(angel*PAI/180);
                      
          break;
                  
          case TAN:
                      result 
          = Math.tan(angel*PAI/180);
                      
          break;
                  }

                  System.out.println(
          "Result< "+ function + " " + angel + " = " + result);
              }

              
              
          /**
               * ---------------------------------------------
               * Method Name : main 
               * Exposition : 測試用主函數
               * ---------------------------------------------
               
          */

              
          public static void main(String[] args){
                  
          new YW5_Test01();
              }

              
          }


           



          歡迎來訪!^.^!
          本BLOG僅用于個人學習交流!
          目的在于記錄個人成長.
          所有文字均屬于個人理解.
          如有錯誤,望多多指教!不勝感激!

          Copyright © 久城

          主站蜘蛛池模板: 通化市| 莒南县| 镇雄县| 航空| 比如县| 特克斯县| 阿城市| 越西县| 沂水县| 稻城县| 元江| 凤阳县| 海宁市| 德兴市| 东港市| 沂南县| 晋城| 温州市| 沿河| 三都| 罗田县| 永善县| 虎林市| 迭部县| 太康县| 秦皇岛市| 拉孜县| 红安县| 正宁县| 靖江市| 平果县| 科尔| 抚州市| 邻水| 中西区| 海淀区| 隆尧县| 灌南县| 奉化市| 扎赉特旗| 新疆|