2014-02-08 75 views
0

我最近着手進入Java編程,並認爲自己是編程新手。這似乎是我的源代碼算術問題。我已經驗證了所有嵌套的if-else語句,並且它們都運行到最後的else語句的算術運算。它不能正確計算我已經設置了上面的if-else語句。初學Java程序員算術錯誤

else語句是假設從量減去40再申請1項%的費用。我曾嘗試else語句fee = ((checkAmount - 40) * .01)fee = ((checkAmount * .01) - 40)

這只是一個練習從書

import java.util.Scanner; 
public class ServiceCharge { 
public static void main(String[] args) 
{ 
    double checkAmount; 
    double fee; 
    Scanner kb = new Scanner(System.in); 
    System.out.println("I will calulate the service charge to cash your check"); 
    System.out.print("Enter amount of check: $"); 
    checkAmount = kb.nextDouble(); 

    if (checkAmount > 0) 
    { 
     if (checkAmount <= 10) 
     { 
      fee = -1; 
      System.out.println("$1 service charge"); 
      checkAmount = checkAmount + fee; 
      System.err.println("You have " + checkAmount + " left after service charge."); 
     } 
     else if ((checkAmount > 10) && (checkAmount <= 100)) 
     { 
      System.out.println("There will be a 10 percent charge."); 
      fee = (checkAmount * .10); 
      checkAmount = checkAmount - fee; 
      System.out.printf("Processing fee: $%.2f\n" , fee); 
      System.out.printf("Check amount: $%.2f\n" , checkAmount); 
     } 
     else if ((checkAmount > 100) && (checkAmount <= 1000)) 
     { 
      System.out.println("There will be a $5 charge plus 5 percent"); 
      fee = ((checkAmount - 5) * .05); 
      checkAmount = (checkAmount - fee); 
      System.out.printf("Processing fee: $%.2f\n" , fee); 
      System.out.printf("Check amount: $%.2f\n", checkAmount); 
     } 
     else 
     { 
      System.out.println("$40 processing fee plus 1 percent"); 
      fee = ((checkAmount - 40) * .01); 
      checkAmount = (checkAmount - fee); 
      System.out.printf("Processing fee: $%.2f\n" , fee); 
      System.out.printf("Check amount: $%.2f\n" , checkAmount); 
     } 
     System.out.println("Thanks for using Service Charger." + "\nGood bye"); 
    } 
} 
} 
+0

究竟發生了什麼?什麼明顯出錯?你有錯誤信息嗎?如果是這樣,請給我們看。你得到錯誤的結果嗎?如果是這樣,請將它們展示給我們,並告訴我們應該發生什麼。 – user2357112

+0

你有多遠?你知道浮點錯誤嗎? http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html – jeremyjjbrown

+1

40加1%的費用是'fee = 40 + checkAmount * 0.01; checkAmount - =費用;',對吧? – cactus1

回答

0
  System.out.println("$40 processing fee plus 1 percent"); 
      fee = ((checkAmount - 40) * .01); 

這不是一個40美元的手續費+ 1%。這是一個略低於1%的費用;就好像您先免費兌換了前40美元,然後再向其餘部分收取1%的費用。

假設1%收費適用於整個支票,而不是什麼減去40元后離開,對於收費的正確表達是

  fee = 40 + 0.01*checkAmount; 
+0

感謝您的澄清。 – user119071

0

其實,你想只在1%收取費用取適量少了$ 40美元,根據您的原始表達式的固定金額,這樣的表達應該是:

fee = 40 + (checkAmount - 40) * .01; 

有沒有在你的代碼,這使得它很難看到發生了什麼事情很多重複的,如果你決定改變 - 例如 - mes你想要展示給用戶的聖人,你現在需要在4個地方改變它,並且有一個很大的改變,你忘了在某處做什麼,或者你犯了一個錯字。

一個良好的編程的目標是避免重複儘可能。

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

     Scanner kb = new Scanner(System.in); 
     System.out.println("I will calulate the service charge to cash your check"); 
     System.out.print("Enter amount of check: $"); 
     double checkAmount = kb.nextDouble(); 

     if (checkAmount > 0) { 
      double fee; 
      String feeMessage; 
      if (checkAmount <= 10) { 
       fee = 1; 
       feeMessage = "$1 service charge"; 
      } else if ((checkAmount > 10) && (checkAmount <= 100)) { 
       feeMessage = "10 percent charge."; 
       fee = (checkAmount * .10); 
      } else if ((checkAmount > 100) && (checkAmount <= 1000)) { 
       feeMessage = "$5 charge plus 5 percent"; 
       fee = 5 + ((checkAmount - 5) * .05); 
      } else { 
       feeMessage = "$40 processing fee plus 1 percent"; 
       fee = 40 + ((checkAmount - 40) * .01); 
      } 
      checkAmount = checkAmount - fee; 

      System.out.printf("Fee structure: " + feeMessage); 
      System.out.printf("Processing fee: $%.2f\n", fee); 
      System.out.printf("Check amount: $%.2f\n", checkAmount); 
      System.out.println("Thanks for using Service Charger.\nGood bye"); 
     } 
    } 
} 

下一步你可能想要研究的是將你的程序在功能上分解爲函數。例如,您要求金額的部分,計算的部分以及顯示結果的部分是三個非常不同的部分。您可能需要單獨更改這三個選項 - 您可能希望從文件或Web請求中獲取輸入,並且您可能希望將結果存儲在另一個計算中,而不是顯示給用戶。

所以這些可以進入不同的功能。

然後你會考慮對象分解 - 也許我們在這裏談論的是有checkAmountfeefeeStructurepayoutAmount性能CheckCashAction對象。然後

等等

的好處是,你可以看看每個步驟單獨地,孤立地進行測試,這使得它更容易找出錯誤和維護代碼。

4

對於最後一個else語句,它從你的剩餘語句,似乎有點過。您使用「保留」來存儲原始的checkAmount值,然後修改checkAmount作爲前三個報表的費用。你應該建模最後一個像之前的那個。checkAmount應該是checkAmount = (checkAmount * .01) + 40,然後hold - checkAmount應返回您正在查找的值。通過擁有checkAmount = checkAmount - 40,最後一行返回保持(checkAmount) - (checkAmount - 40),它將始終返回40