練習(xí)題---三角運(yùn)算

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

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

          代碼如下:
          /**---------------------------------------------
           *   Class Name   : YW2_Test01.java
           *   Purpose      : 編寫一個小的計算程序,用來進(jìn)行三角運(yùn)算(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;

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


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

              
          /**
               * ---------------------------------------------
               * Method Name : YW5_Test01 
               * Exposition : 構(gòu)造函數(shù),執(zhí)行運(yùn)算過程
               * ---------------------------------------------
               
          */

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

              
              
          /**
               * ---------------------------------------------
               * Method Name : setFuncName 
               * Exposition : 設(shè)置三角函數(shù)名字
               * ---------------------------------------------
               
          */

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

              
          /**
               * ---------------------------------------------
               * Method Name : setFunction 
               * Exposition : 設(shè)置三角函數(shù)名字
               * ---------------------------------------------
               
          */

              
          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 : 取得三角函數(shù)名字
               * ---------------------------------------------
               
          */

              
          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 : 檢查三角函數(shù)名字
               * ---------------------------------------------
               
          */

              
          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 : 設(shè)置角度
               * ---------------------------------------------
               
          */

              
          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 : 取得結(jié)果
               * ---------------------------------------------
               
          */

              
          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 : 測試用主函數(shù)
               * ---------------------------------------------
               
          */

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

              
          }


           



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

          Copyright © 久城

          主站蜘蛛池模板: 兴宁市| 乳源| 朝阳市| 密山市| 密云县| 梅州市| 芮城县| 辉南县| 石景山区| 建德市| 屯留县| 开鲁县| 阿克| 普定县| 海林市| 崇明县| 灵台县| 小金县| 巴彦淖尔市| 开封市| 广宗县| 瓮安县| 拜泉县| 保靖县| 汶川县| 房山区| 芜湖市| 章丘市| 原阳县| 普兰县| 拉孜县| 大荔县| 略阳县| 昌宁县| 广元市| 武隆县| 虞城县| 弋阳县| 扎鲁特旗| 秦安县| 莱州市|