網頁

2015年4月16日 星期四

期中程式

鏈接:程式設計工藝大師
程式內容:工程計算器
程式代碼:

import java.lang.*;
import java.util.Scanner;

public class calculator {
    public static void main(String args[]) {

        // Java.util.Scanner
        Scanner keyin = new Scanner(System.in);
        double value, ans=0;
        int select;
        disp_men ();

        //keyin  nextInt()
        select = keyin.nextInt();
        while ( select != 7) {
                 System.out.println("輸入一個數值 (注意三角函數範圍) =>");

                value = keyin.nextDouble();
                switch(select){
                case 1:
                        ans = Math.log(value);
                        System.out.printf("log(%,2f) = %.2f\n", value, ans);
                        break;
                case 2:
                        ans = Math.log10(value);
                        System.out.printf("log10(%,2f) = %.2f\n", value, ans);
                        break;
                case 3:
                        ans = Math.sqrt(value);
                        System.out.printf("sqrt(%,2f) = %.2f\n", value, ans);
                        break;
                case 4:
                        ans = Math.sin(value);
                        System.out.printf("sin(%,2f) = %.2f\n", value, ans);
                        break;
                case 5:
                        ans = Math.cos(value);
                        System.out.printf("cos(%,2f) = %.2f\n", value, ans);
                        break;
                case 6:
                        ans = Math.tan(value);
                        System.out.printf("tan(%,2f) = %.2f\n", value, ans);
                        break;
                default:
                        System.out.printf("sin(%,2f) = %.2f\n", value, ans);
                }
                disp_men();
               select = keyin.nextInt();
        }
        System.out.print("== 謝謝使用 ==\n");
}
static void disp_men() {
        System.out.printf("\n*** 工程計算器提供有下列功能 ***\n");
        System.out.printf("(1) log  函數\t (2) log10() 函數\n");
        System.out.printf("(3) sqrt 函數\t (2) sin() 函數\n");
        System.out.printf("(5) cos  函數\t (2) tan() 函數\n");
        System.out.printf("(7) 離開系統");
        System.out.printf("請選擇輸入 =>");

    }
}





沒有留言:

張貼留言