布尔值存储不正确



我编写的程序没有正确存储布尔值late,每次都将其设置为true,而不管输入了什么("是"或"否")。因为每次程序运行时,它都会将其存储为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.2fn", lateFee);
            }    
        }
    } else {
        System.out.println("Interest rate: " + interestOutput + "% per month");
    }
    } //  End public static void main method
} //  End public class Discount

if语句中,您将true分配给late,而不是与其进行比较。请执行以下操作:

if(late == true)

在我看来,您似乎在检查latePayment.equals(lateTrue)是否为true,但变量lateTrue包含一个字符串,而不是布尔值。

最新更新