我最近着手進入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");
}
}
}
究竟發生了什麼?什麼明顯出錯?你有錯誤信息嗎?如果是這樣,請給我們看。你得到錯誤的結果嗎?如果是這樣,請將它們展示給我們,並告訴我們應該發生什麼。 – user2357112
你有多遠?你知道浮點錯誤嗎? http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html – jeremyjjbrown
40加1%的費用是'fee = 40 + checkAmount * 0.01; checkAmount - =費用;',對吧? – cactus1