英文:
got error "incompatible types: void cannot be converted to int" in OOP
问题
I'll provide the translation for the code-related content:
我对Java和stackoverflow都很新,对于任何混淆,我感到抱歉。以下是我的代码,我遇到了以下错误:
./LETest.java:8: error: incompatible types: void cannot be converted to int
int returned = savingsTest.withdraw(200);
^
./LETest.java:18: error: incompatible types: void cannot be converted to int
int returned = savingsTest.withdraw(500);
^
2 errors
但是,IDE确实打印出了以下结果:
Hello!
您的余额是
您刚刚存入了500
您刚刚提取了200
代码:
public class SavingsAccount {
int balance;
int amountToDeposit;
int amountToWithdraw;
// 初始余额
public SavingsAccount(int initialBalance){
balance = initialBalance;
}
// 检查余额
public void checkBalance(){
System.out.println("Hello!");
System.out.println("您的余额是 " + balance);
}
// 存款
public void deposit(int amountToDeposit){
balance = balance + amountToDeposit;
System.out.println("您刚刚存入了 " + amountToDeposit);
}
// 提取
public void withdraw(int amountToWithdraw){
balance = balance - amountToWithdraw;
System.out.println("您刚刚提取了 " + amountToWithdraw);
}
public static void main(String[] args){
SavingsAccount savings = new SavingsAccount(2000);
savings.checkBalance();
savings.deposit(500);
savings.withdraw(200);
}
}
请问您需要其他帮助吗?
英文:
I'm pretty new to Java and stackoverflow so sorry for any confusions. Below is my code and I got the following errors:
./LETest.java:8: error: incompatible types: void cannot be converted to int
int returned = savingsTest.withdraw(200);
^
./LETest.java:18: error: incompatible types: void cannot be converted to int
int returned = savingsTest.withdraw(500);
^
2 errors
The IDE did print out the following results though:
Hello!
Your balance is
You just deposited 500
You just withdrew 200
Code:
public class SavingsAccount {
int balance;
int amountToDeposit;
int amountToWithdraw;
// initial balance
public SavingsAccount(int initialBalance){
balance = initialBalance;
}
// check balance
public void checkBalance(){
System.out.println("Hello!");
System.out.println("Your balance is " + balance);
}
// deposit
public void deposit(int amountToDeposit){
balance = balance + amountToDeposit;
System.out.println("You just deposited " + amountToDeposit);
}
// withdraw
public void withdraw(int amountToWithdraw){
balance = balance - amountToWithdraw;
System.out.println("You just withdrew " + amountToWithdraw);
}
public static void main(String[] args){
SavingsAccount savings = new SavingsAccount(2000);
savings.checkBalance();
savings.deposit(500);
savings.withdraw(200);
}
}
Would someone mind helping?
I tried changing void to int in the withdraw() method and got a new error
SavingsAccount.java:30: error: missing return statement
}
^
1 error
答案1
得分: 1
你知道你想要将withdraw
的返回类型更改为int
。你也知道你漏掉了一个return
语句。你可能想要返回*newBalance
*。所以添加它。像这样,
// 取款。接受一个整数。返回一个整数。
public int withdraw(int amountToWithdraw) {
int newBalance = balance - amountToWithdraw;
balance = newBalance;
System.out.println("您刚刚取款了 " + amountToWithdraw);
return newBalance; // 要返回的整数。
}
英文:
You know you want to change the return type of withdraw
to int
. And you know you were missing a return
. Presumably you want to return the newBalance
. So add that. Like,
// Withdraw. Take an int. Return an int.
public int withdraw(int amountToWithdraw) {
int newBalance = balance - amountToWithdraw;
balance = newBalance;
System.out.println("You just withdrew " + amountToWithdraw);
return newBalance; // The int to return.
}
答案2
得分: 1
以下是翻译好的部分:
"Your code is slightly different than what is reported in the error."
这部分不需要翻译。
"These errors are reporting from lines 8 and 18."
这部分不需要翻译。
"Which, is not in the code you provided."
这部分不需要翻译。
"Nevertheless, I'll explain the situation you encountered."
这部分不需要翻译。
"In Java, a method is declared with a return type."
"If you don't intend on returning a value, you can use void."
"Defining Methods (The Java™ Tutorials > Learning the Java Language > Classes and Objects)."
这部分不需要翻译。
"The error you're encountering is because you're attempting to assign a void to an int."
"The withdraw method is declared as void."
这部分不需要翻译。
"You mention the following."
"> "I tried changing void to int in the withdraw() method and got a new error""
这部分不需要翻译。
"The error encountered here is because, if a method is declared with a return type, you must use the return keyword, and the value."
"Returning a Value from a Method (The Java™ Tutorials > Learning the Java Language > Classes and Objects)."
这部分不需要翻译。
"For example, if you wanted to return a value from the withdraw method, you could use the following."
这部分不需要翻译。
"Additionally, the correct accounting term would be, withdrawal."
这部分不需要翻译。
英文:
Your code is slightly different than what is reported in the error.
./LETest.java:8: error: incompatible types: void cannot be converted to int
int returned = savingsTest.withdraw(200);
^
./LETest.java:18: error: incompatible types: void cannot be converted to int
int returned = savingsTest.withdraw(500);
^
2 errors
These errors are reporting from lines 8 and 18.
int returned = savingsTest.withdraw(200);
int returned = savingsTest.withdraw(500);
Which, is not in the code you provided.
Nevertheless, I'll explain the situation you encountered.
In Java, a method is declared with a return type.
If you don't intend on returning a value, you can use void.
Defining Methods (The Java™ Tutorials > Learning the Java Language > Classes and Objects).
The error you're encountering is because you're attempting to assign a void to an int.
The withdraw method is declared as void.
You mention the following.
> "I tried changing void to int in the withdraw() method and got a new error"
The error encountered here is because, if a method is declared with a return type, you must use the return keyword, and the value.
Returning a Value from a Method (The Java™ Tutorials > Learning the Java Language > Classes and Objects).
For example, if you wanted to return a value from the withdraw method, you could use the following.
public int withdraw(int amountToWithdraw){
balance = balance - amountToWithdraw;
System.out.println("You just withdrew " + amountToWithdraw);
return balance;
}
Additionally, the correct accounting term would be, withdrawal.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论