我如何修复Java:26:错误:找不到合适的构造函数

时间:2015-06-28 08:18:42

标签: java compiler-errors

我是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());
    }
}

2 个答案:

答案 0 :(得分:0)

您的代码存在多个问题。

您通常不会使用static块。您可能想要写一个public static void main(String... args)

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

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

答案 1 :(得分:0)

代码中有2个错误:

关于第一个错误Java编译器指出,在超类BankAccount中,您没有包含3个变量的构造函数(在super(name, balance, interestRate);类中调用SavingAccounts。您应该:

  • 将相应的构造函数添加到BankAccount类或
  • 使用BankAccount类中的现有构造函数,并将interestRate保留为SavingAccounts类变量。

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