I can't seem to find the reason why its "error: cannot find symbol"
I can't seem to find the reason why its "error: cannot find symbol"
[免责声明:初学者 Java]
我的程序 运行 刚刚遇到问题,而我正试图完成它。错误是它找不到符号,IDE (我正在使用 Text Pad) 在 main 方法中精确定位它,检查下面:
BalanceW.java:22: error: cannot find symbol
account.withdraw(500.00);
^
symbol: method withdraw(double)
location: variable account of type Account
如果你想知道代码,给你:
Account.java
public class Account{
private String accntNumber;
private String accntName;
private double balance;
public Account(){}
public Account(String num, String name, double bal){
accntNumber = num;
accntName = name;
balance = bal;
}
public double getBalance(){ return balance;}
}
BalanceW.java (我把main方法压缩在了Balance W里面所以post不会太长)
public class BalanceW extends Account{
public double withdraw(double amount){
double bal = getBalance();
if(amount <= 0){
throw new ArithmeticException("Invalid amount: Amount is less than 0");
}
if(amount > bal){
throw new ArithmeticException("Insufficient: Insufficient funds");
}
bal = bal - amount;
return amount;
}
public static void main(String[] args){
Account account = new Account("Acct-001","Juan dela Cruz", 5000.0);
account.withdraw(500.00);
System.out.println("Balance: "+account.getBalance());
}
}
我只想知道怎么出错,为什么出错。公平地说,我查找了多个选项卡以寻找修复错误的方法。非常感谢您阅读 post,如果您能帮助我解决这个问题,我将不胜感激。
withdraw 方法是为 BalanceW 定义的,而不是为 Account 定义的。如果你要使用这个方法,在方法main的第一行你需要声明账户为BalanceW,如下所示:
BalanceW account = new BalanceW("Acct-001", "Juan dela Cruz", 5000.0);
account.withdraw(500.00);
[免责声明:初学者 Java]
我的程序 运行 刚刚遇到问题,而我正试图完成它。错误是它找不到符号,IDE (我正在使用 Text Pad) 在 main 方法中精确定位它,检查下面:
BalanceW.java:22: error: cannot find symbol
account.withdraw(500.00);
^
symbol: method withdraw(double)
location: variable account of type Account
如果你想知道代码,给你:
Account.java
public class Account{
private String accntNumber;
private String accntName;
private double balance;
public Account(){}
public Account(String num, String name, double bal){
accntNumber = num;
accntName = name;
balance = bal;
}
public double getBalance(){ return balance;}
}
BalanceW.java (我把main方法压缩在了Balance W里面所以post不会太长)
public class BalanceW extends Account{
public double withdraw(double amount){
double bal = getBalance();
if(amount <= 0){
throw new ArithmeticException("Invalid amount: Amount is less than 0");
}
if(amount > bal){
throw new ArithmeticException("Insufficient: Insufficient funds");
}
bal = bal - amount;
return amount;
}
public static void main(String[] args){
Account account = new Account("Acct-001","Juan dela Cruz", 5000.0);
account.withdraw(500.00);
System.out.println("Balance: "+account.getBalance());
}
}
我只想知道怎么出错,为什么出错。公平地说,我查找了多个选项卡以寻找修复错误的方法。非常感谢您阅读 post,如果您能帮助我解决这个问题,我将不胜感激。
withdraw 方法是为 BalanceW 定义的,而不是为 Account 定义的。如果你要使用这个方法,在方法main的第一行你需要声明账户为BalanceW,如下所示:
BalanceW account = new BalanceW("Acct-001", "Juan dela Cruz", 5000.0);
account.withdraw(500.00);