class BankAccount {
private String firstname;
private String lastname;
private int ssn;
private int accountnumber = 0;
private double accountbalance = 0.0;
BankAccount() {
firstname = "John Smith";
}
BankAccount(String firstname, String lastname, int ssn) {
int accountnumber;
double accountbalance;
}
public String getName() {
return firstname;
}
public double getBalance() {
return accountbalance;
}
public void setAccountNumber(int accountnumber) {
return accountnumber;
}
public boolean equals(BankAccount ba) {
return this.accountbalance == ba.accountbalance;
}
public void deposit(double amount) {
return this.accountbalance = this.accountbalance + amount;
}
public int withdraw(double amount) {
this.accountbalance = this.accountbalance - amount;
if(accountbalance < 0) {
this.accountbalance = this.accountbalance + amount;
return -1;
}
else
return 0;
}
public String toString() {
return firstname+lastname+"/n"+"social secuirty "+"ssn"+"/n"+"account number is "+
"accountnumber" +"/n" +"Balance:" +"accountbalance" +"/n";
}
}
Run Code Online (Sandbox Code Playgroud)
错误是:
BankAccount.java:34: error: incompatible types: unexpected return value
return accountnumber;
^
BankAccount.java:47: error: incompatible types: unexpected return value
return this.accountbalance=this.accountbalance+amount;
^
2 errors
Run Code Online (Sandbox Code Playgroud)
我做错了什么?
一种void方法不能return有所作为.并且具有返回类型的方法必须return是方法签名中指定的类型.
错误1
例如,看看这个方法
public void setAccountNumber(int accountnumber) {
return accountnumber;
}
Run Code Online (Sandbox Code Playgroud)
哟不能从一个void方法返回.
那应该是
public void setAccountNumber(int accountnumber) {
this.accountnumber =accountnumber;
}
Run Code Online (Sandbox Code Playgroud)
其余方法也是如此.
错误2
public void deposit(double amount) {
return this.accountbalance = this.accountbalance + amount;
}
Run Code Online (Sandbox Code Playgroud)
返回语句在语法上是错误的.你无法返回,因为它是无效的.那应该是
public void deposit(double amount) {
this.accountbalance = this.accountbalance + amount;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
30093 次 |
| 最近记录: |