How do I fix a Java:26: error: No suitable constructor found

How do I fix a Java:26: error: No suitable constructor found

我是 Java 编写脚本的新手,我正在尝试修复我在编译代码时遇到的四个错误。我很沮丧,我做错了什么?

错误是:

SavingsAccount.java:25: error: no suitable constructor found for BankAccount(String,double,double)
super(name, balance, interestRate);
^
constructor BankAccount.BankAccount(String,double) is not applicable
(actual and formal argument lists differ in length)
constructor BankAccount.BankAccount(String) is not applicable
(actual and formal argument lists differ in length)
SavingsAccount.java:29: error: cannot find symbol
Interest = balance * (interestRate / 12);
^
symbol: variable Interest
location: class SavingsAccount
SavingsAccount.java:29: error: cannot find symbol
Interest = balance * (interestRate / 12);
^
symbol: variable interestRate
location: class SavingsAccount
SavingsAccount.java:31: error: cannot find symbol
this.deposit(interest);
^
symbol: variable interest
location: class SavingsAccount
4 errors

我的代码:

import java.util.Scanner; // Needed for the Scanner class
import java.text.DecimalFormat; // Needed for 2 decimal place amounts
import java.util.logging.Level;
import java.util.logging.Logger;

class SavingsAccount extends BankAccount {
    static {
        BankAccount account; // To reference a BankAccount object
        double balance, // The account's starting balance
        interestRate; // The annual interest rate
        // Create a Scanner object for keyboard input.
        Scanner keyboard = new Scanner(System.in);
        // Create an object for dollars and cents
        DecimalFormat formatter = new DecimalFormat("#0.00");
        // Get the starting balance.
        // Get the annual interest rate.
        interestRate = keyboard.nextDouble() * .001;
        // post monthly interest by multiplying current balance
        // by current interest rate divided by 12 and then adding
        // result to balance by making deposit
    }

    public SavingsAccount(String name, double balance, double interestRate)
            throws NegativeAmountException {
        super(name, balance, interestRate);
    }

    public void postInterest() {
        Interest = balance * (interestRate / 12);
        try {
            this.deposit(interest);
            // and current account balance (use printStatement from
            // the BankAccount superclass)
            // following this also print current interest rate
        } catch (Exception ex) {
            Logger.getLogger(SavingsAccount.class.getName()).log(Level.SEVERE,
                    null, ex);
        }
    }
}

BankAccount.java(这个没有错误):

class BankAccount {
    public String name;
    public double balance;

    public BankAccount(String name, double balance) throws NegativeAmountException {
        this.name = name;
        this.balance = balance; // set name and balance 
        if (balance < 0) { // make sure balance is not negative
            throw new NegativeAmountException("Can not create an account with a negative amount."); // throw exception if balance is negative
        }
    }

    public BankAccount(String name) throws NegativeAmountException {
        this(name, 0); // set name and use 0 balance 
    }

    // update balance by adding deposit amount
    // make sure deposit amount is not negative 
    // throw exception if deposit is negative 
    public void deposit(double amount) throws NegativeAmountException {
        if (amount > 0) {
            this.balance += amount;
        }
        else {
            throw new NegativeAmountException("Deposit amount must not be negative");
        }
    }

    // update balance by subtracting withdrawal amount
    // throw exception if funds are not sufficient
    // make sure withdrawal amount is not negative 
    // throw NegativeAmountException if amount is negative 
    // throw InsufficientFundsException if balance < amount
    public void withdraw(double amount) throws InsufficientFundsException, NegativeAmountException {
        if (amount > getBalance()) {
            throw new InsufficientFundsException("You do not have sufficient funds for this operation.");
        }
        else if (amount < 0) {
            throw new NegativeAmountException("Withdrawal amount must not be negative.");
        }
        else {
            this.balance -= amount;
        }
    }

    // return current balance
    public double getBalance() {
        return this.balance;
    }

    // print bank statement including customer name
    // and current account balance
    public void printStatement() {
        System.out.println("Balance for " + this.name + ": " + getBalance());
    }
}

您的代码存在多个问题。

您通常不会像现在这样使用 static 块。你可能想写一个 public static void main(String... args).

您的代码的实际问题是通过 super(...) 调用超类构造函数。 BankAccount 不提供具有指定签名的可访问构造函数 (String, double, double)。要么没有这样的构造函数,要么构造函数不可见(详见this)。 有关 Interest 的其他错误,请查看

此外,Java有一些普遍接受的编码标准,一些可以找到here。请尊重他们。

您的代码有 2 个错误:

关于第一个错误 Java 编译器注意到在 superclass BankAccount 中你没有带 3 个变量的构造函数(你在 SavingAccounts 中的调用 super(name, balance, interestRate); class。你应该:

  • BankAccountclass或
  • 中添加对应的构造函数
  • 使用来自 BankAccount class 的现有构造函数并将 interestRate 保留为 SavingAccounts class 变量。

编译器认为第二个错误导致 3 个错误:在代码中 Interest = balance * (interestRate / 12); 您丢失了局部变量。修复后(似乎应该是 double interest = balance * (interestRate / 12);),您将删除 3 个错误。