我应该开设一个班级和一个测试员班级,允许客户输入他的姓名和当前金额以及他想进行的任何存款或取款。可能是不正确的代码,但是我想知道我在用代码做错什么。问题之一是它说getBalance是无效的方法声明。

这是带有构造函数的类

public class BankAccount{
    public String name;
    public double Balance, Withdrawal, Deposit;

    public BankAccount(double bal, String nm){
        name = nm;
        Balance = bal;
    }


    public void setName (String nm){
        name = nm;
    }

    public String getName(){
        return name;
    }

    public String toString(){
        return "Name:    " + name + "\n" +
        "New Balance :     " + getBalance;
    }

    public getBalance(){
        return Balance;
    }

    public double Withdrawal(){
        bal = Balance - Withdrawal;
    }

    public double Deposit(){
        bal = Balance + Deposit;
    }
}


这是测试人员类

import java.util.Scanner;

public class BankAccountTester{
    public static void main(String [] args){
        Scanner reader = new Scanner(System.in);
        a1 = new BankAccount();

        String name;
        int Balance, Withdrawal, Deposit;

        System.out.println("What is the name of the bank account holder?");
        name = reader.nextLine();

        System.out.println("What is your current balance?");
        Balance = reader.nextInt();

        System.out.println("How much do you wish to deposit?");
        Deposit = reader.nextInt();

        System.out.println("How much do you with to withdraw?");
        Withdrawal = reader.nextInt();

        System.out.println("\n" + a1.getName());

    }
}


您介意告诉我我在做什么错并加以解释吗?

最佳答案

getBalance()方法的返回类型丢失,而您返回的是double类型的变量。声明为:-

public double getBalance(){   // add double as the return-type here
    return Balance;
}


此外,正如Drew Kennedy提到的那样,通过将BankAccount类与a1一起添加来声明BankAccount类的对象。你没在任何地方提到它...

BankAccount a1 = new BankAccount();  // you're missing class name BankAccount

07-27 13:31