Anonymous
Anonymous asked in 電腦與網際網路程式設計 · 1 decade ago

超急件...一Java程式題目求解

http://img43.imageshack.us/img43/3333/asdumx.jpg

圖一: 數值運算有時會產生些微的誤差

BigDecimal b1 = new BigDecimal(“0.01”); //產生0.01 的BigDecimal 物件

BigDecimal b2 = new BigDecimal(“0.09”); //產生0.09 的BigDecimal 物件

BigDecimal result = b1.add(b2); //使用方法add 進行相加的運算

System.out.println(result); //輸出結果

其他關於兩數相減、相乘、相除等運算方式,請參考Java 的API 尋求上述類似

的作法,其中除法的部分,為了讓我們可指定小數點位數的計算範圍(scale),請

選擇用下列的物件方法來做,其中進位(rounding)的模式請使用HALF_EVEN(四

捨六入五成雙):

divide(BigDecimal divisor, int scale, RoundingMode roundingMode)

程式的執行畫面請參考下面圖二及圖三所示(加法及減法的格式和乘法一樣):

http://img43.imageshack.us/img43/4893/addzbq.jpg

圖二: 使用者選擇執行乘法運算

http://img43.imageshack.us/img43/7814/assitg.jpg

圖三: 執行除法運算,使用者需要輸入小數點位數

A、B 值以及運算模式為使用者所輸入(包括除法的小數點位數)

輸出格式須符合上面的範例,注意除法需要多一項輸入(小數點位數)

檢查使用者輸入的運算模式是否為加減乘除(+ - * /)的其中一種

程式碼需要適當的縮排以及加上註解(Comments)

急件...請會的大大幫解...

2 Answers

Rating
  • CK
    Lv 4
    1 decade ago
    Best Answer

    import java.io.*;

    import java.math.*;

    public class BigDecimalOperation {

    public BigDecimalOperation() {

    }

    public void plus(double a1, double a2) {

    BigDecimal b1 = new BigDecimal(a1);

    BigDecimal b2 = new BigDecimal(a2);

    BigDecimal rst = b1.add(b2);

    out(a1+" + "+a2+" = "+rst);//輸出結果

    }

    public void minus(double a1, double a2) {

    BigDecimal b1 = new BigDecimal(a1);

    BigDecimal b2 = new BigDecimal(a2);

    BigDecimal rst = b1.subtract(b2);

    out(a1+" - "+a2+" = "+rst);//輸出結果

    }

    public void multiply(double a1, double a2) {

    BigDecimal b1 = new BigDecimal(a1);

    BigDecimal b2 = new BigDecimal(a2);

    BigDecimal rst = b1.multiply(b2, new MathContext(BigDecimal.ROUND_HALF_UP));

    out(a1+" * "+a2+" = "+rst);//輸出結果

    }

    public void divide(double a1, double a2, int scale) {

    BigDecimal b1 = new BigDecimal(a1);

    BigDecimal b2 = new BigDecimal(a2);

    BigDecimal rst = b1.divide(b2, scale, BigDecimal.ROUND_HALF_EVEN);

    out(a1+" / "+a2+" = "+rst);//輸出結果

    }

    ... ... ...

    2009-06-04 04:59:57 補充:

    too bad, can't post all of the content.

    2009-06-04 05:03:05 補充:

    public void doOperation() throws Exception {

    out("請輸入兩個數字: (整數或浮點數皆可) ");

    System.out.print("A = ");

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    double a1 = Double.valueOf(br.readLine());

    System.out.print("B = ");

    double a2 = Double.valueOf(br.readLine());

    2009-06-04 05:03:44 補充:

    System.out.print("請輸入運算模式: (+ - * /): ");

    String operator = br.readLine();

    if(operator.equals("+")){

    this.plus(a1, a2);

    } else if(operator.equals("-")){

    this.minus(a1, a2);

    } else if(operator.equals("*")){

    this.multiply(a1, a2);

    }

    2009-06-04 05:04:47 補充:

    } else if(operator.equals("/")){

    System.out.print("小數點位數 ");

    int scale = Integer.valueOf(br.readLine());

    this.divide(a1, a2, scale);

    }

    br.close();

    }

    2009-06-04 05:05:06 補充:

    public static void main(String[] args) throws Exception {

    BigDecimalOperation decimal = new BigDecimalOperation();

    decimal.doOperation();

    System.out.println("");

    System.out.println("Process completed.");

    }

    private void out(String s) {

    System.out.println(s);

    }

    }

    Source(s): Me
  • 5 years ago

    所以如果你想婚的念頭已經確定

    就去找一家評價好一點的婚友社!

    行動吧!

    這是我之前參加的婚友社我覺得不錯

    或搜尋 紅娘李姐

Still have questions? Get your answers by asking now.