java - 如何修复 Java :26: error: No suitable constructor found

标签 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 个错误:

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

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

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

关于java - 如何修复 Java :26: error: No suitable constructor found,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31097529/

相关文章:

java - ANT - 如何使用 exclude,excludesfile 与 javac?

Java 按钮悬停

java - 动态速率限制器以避免数据库限制

swift - 为 REST API datatask 创建端点 URL 时出现 xcode 错误 'consecutive statements on a line must be separated by'

android - 在编译期间,如何修复 android studio 中 Room 中复杂 sql 语句验证的错误?

java - 当我尝试在 NetBeans 上运行简单程序时出现错误 "No main class found"。

java - J2EE Web 应用程序的聊天服务器插件 API

java - 如何根据生产者线程状态停止消费者线程

c++ - 错误CC2061 : syntax error: identifier 'GLUquadric'

java - 编译从Java中的Swing的JFrame类返回缺少的方法和包