gpt4 book ai didi

java - 无法让我的 Java 类工作

转载 作者:行者123 更新时间:2023-11-30 03:39:37 27 4
gpt4 key购买 nike

我应该创建一个类和一个测试类,允许客户输入他的姓名和当前金额以及他想要进行的任何存款或取款。可能有不正确的代码,但我想知道我的代码做错了什么。问题之一是它说 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());

}
}

你介意告诉我我做错了什么并解释一下吗?

最佳答案

当您返回 double 类型的变量时,getBalance() 方法的返回类型丢失。将其声明为:-

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 

关于java - 无法让我的 Java 类工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27070622/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com