我最近开始从事Java编程,并认为自己是编程新手。看来我的源代码算法存在问题。我已经验证了所有嵌套的if-else语句,并且它们都符合最后的else语句的算术。它没有正确计算我已经将算法设置为上面的if-else语句。
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 :(得分:4)
对于最后一个else语句,它似乎与其余的陈述有点不同。您使用“hold”存储原始checkAmount
值,然后将checkAmount
修改为前三个语句的费用。你应该像前一个那样建模最后一个。 checkAmount
应为checkAmount = (checkAmount * .01) + 40
,然后hold - checkAmount
应返回您要查找的值。通过checkAmount = checkAmount - 40
,最后一行将返回暂停(checkAmount) - (checkAmount - 40)
,这将始终返回40
。
答案 1 :(得分: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;
答案 2 :(得分:0)
实际上,根据您的原始表达式,您只想收取超过支票金额减去40美元固定费用的1%费用,因此表达式应为:
fee = 40 + (checkAmount - 40) * .01;
您的代码中存在大量重复,这使得更难以查看正在发生的事情,并且如果您决定更改 - 例如 - 要向用户显示的消息,则现在需要更改它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请求中获取输入,并且您可能希望将结果存储在另一个计算中,而不是将其显示给用户。
所以这些可能会分开进行。
然后你可以考虑对象分解 - 也许我们在这里谈论的CheckCashAction
对象具有checkAmount
,fee
,feeStructure
和{{的属性1}}。
等
然后好处是你可以分别查看每个步骤并单独测试它们,这样可以更容易地查明错误并维护代码。