好久沒有做練習(xí)題了。
編寫一個小的計算程序,用來進(jìn)行三角運(yùn)算(Sin, Cos,tan…),該程序通過交互接收用戶輸入,例如:
系統(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();
}
}
編寫一個小的計算程序,用來進(jìn)行三角運(yùn)算(Sin, Cos,tan…),該程序通過交互接收用戶輸入,例如:
系統(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)容
代碼如下:


































































































































































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