我最近开始从事Java编程,并认为自己是一个编程新手。看来我的源代码算法存在问题。我已经验证了所有嵌套的if-else语句,并且它们都符合最后的else语句的算术。它没有正确计算我已经将算法设置为上面的if-else语句。
else语句假设从金额中减去40,然后应用1%的费用。
这只是本书的练习
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");
}
}
}
修订代码