我编写的程序没有正确存储布尔值late
,并且每次都设置为true,无论输入的是什么(“是”或“否”)。因为每次程序运行时它都会将其存储为真,就好像客户每次都有延迟付款一样。我无法盯住我所缺少的东西。
public static void main(String[] args) {
final double PCT1 = .02; // Platinum
final String PLATINUM_LEVEL = "Platinum";
final double PCT2 = .025; // Gold
final String GOLD_LEVEL = "Gold";
final double PCT3 = .03; // Silver
final String SILVER_LEVEL = "Silver";
final double plusInterest = 0.005;
double interest = 0;
double interestOutput = 0;
Scanner keyboard = new Scanner(System.in);
String name; // Name of customer
String level; // Membership level of customer
String lateTrue = "Yes";
String lateFalse = "No";
String latePayment;
boolean late = false;
double lateFee = 25.00;
double percentToPrinciple;
double percentToInterest;
double minPayment = 0;
double balance;
double princePay;
double monthlyPay; //Last Payment late?
System.out.println("Was the last payment made late?");
latePayment = keyboard.next();
if (latePayment.equals(lateTrue)) {
late = true;
}
else if (latePayment.equals(lateFalse)) {
late = false;
} // Interest Rate Calculation
if (level.equals(PLATINUM_LEVEL)) {
interest = PCT1;
if (late = true) {
interest = interest + plusInterest;
interestOutput = 2.5;
}
else {
interestOutput = 2;
}
}
else if (level.equals(GOLD_LEVEL)) {
interest = PCT2;
if (late = true) {
interest = interest + plusInterest;
interestOutput = 3;
}
else {
interestOutput = 2.5;
}
}
else if (level.equals(SILVER_LEVEL)) {
interest = PCT3;
interestOutput = 3;
} // Monthly Payment
monthlyPay = PCT3 * balance; // Payment to Principle Calculation
princePay = balance * interest; // Minimum Payment
minPayment = princePay + monthlyPay;
if (level.equals(SILVER_LEVEL)) {
if (late = true) {
minPayment = minPayment + lateFee;
}
} // percentToPrinciple
percentToPrinciple = 100 * (princePay / minPayment); // precentToInterest
percentToInterest = 100 - percentToPrinciple;
if (late = true) {
System.out.println("Interest rate for late payment: " + interestOutput + "% per month");
if (level.equals(SILVER_LEVEL)) {
if (late = true) {
System.out.printf("Late fee: $" + "%3.2f\n", lateFee);
}
}
} else {
System.out.println("Interest rate: " + interestOutput + "% per month");
}
} // End public static void main method
} // End public class Discount
答案 0 :(得分:1)
在if
声明中,您将 true
分配给late
,而不是与之进行比较。这样做:
if(late == true)
答案 1 :(得分:0)
在我看来,你正在检查latePayment.equals(lateTrue)是否为true,但变量lateTrue包含一个字符串而不是布尔值。