我遇到以下语法的一些问题。
我目前正在学习Java,并且一直在阅读过去的考试试卷,以帮助我建立自己的Java知识。
以下是问题:
编写一个类帐户,其中包含帐号和帐户当前余额的实例变量。实现构造函数和方法getAccountNumber(),getBalance(),debit(double amount)和credit(double amount)。在您的借记和贷记实施中,检查指定的金额是否为正,并且在借方法中不会引起透支。在这些情况下返回false。否则,请更新余额。
我试图这样做但是我没有为借记和贷记方法实现布尔函数。我只是想先建立程序并尝试让它运行起来。我之后会看到这个,因为我不确定如何返回真或假,同时也试图从上述方法返回一定金额。
请原谅我的代码中的任何错误,因为我还在学习Java。
我可以运行我的代码,但是当我输入存款时,它似乎无法正常工作,我将非常感谢此处的任何指示。
这是我的代码:
import java.util.*;
public class Account {
private int accountNumber;
private static double currentBalance;
private static double debit;
// ***** CONSTRUCTOR *****//
public Account(double currentBalance, int accountNumber) {
accountNumber = 12345;
currentBalance = 10000.00;
}
public int getAccountNumber(int accountNumber) {
this.accountNumber = accountNumber;
return accountNumber;
}
public double getcurrentBalance(double currentBalance) {
this.currentBalance = currentBalance;
return currentBalance;
}
public static double debit(double currentBalance, double amount) {
currentBalance -= amount;
return currentBalance;
}
public static double credit(double currentBalance, double amount) {
currentBalance += amount;
return currentBalance;
}
public static void main(String [] args){
String withdraw = "Withdraw";
String deposit = "Deposit";
double amount;
Scanner in = new Scanner(System.in);
System.out.println("Are you withdrawing or depositing? ");
String userInput = in.nextLine();
if(userInput == withdraw)
System.out.println("Enter amount to withdraw: ");
amount = in.nextDouble();
if(amount > currentBalance)
System.out.println("You have exceeded your amount.");
debit(currentBalance, amount);
System.out.println("Your new balance is: " + currentBalance);
if (userInput == deposit)
System.out.println("Enter amount to deposit: ");
amount = in.nextDouble();
credit(currentBalance, amount);
System.out.println("Your new balance is: " + currentBalance);
}
}
请再次原谅我的代码中的任何错误。我还在学习它的语法。
答案 0 :(得分:6)
在if语句if(userInput == withdraw)
中,您试图比较String
个对象。
在Java中比较String
个对象,使用equals
方法而不是比较运算符==
if(userInput.equals(withdraw))
代码中有多个实例使用String
比较==
个对象,将其更改为equals
。
使用条件块时,最好用大括号{}
if(true){
}
答案 1 :(得分:3)
您不使用括号,因此只执行if语句后的第一行。此外,应使用.equals(otherString)
比较字符串。像这样:
if(userInput.equals(withdraw))
System.out.println("Enter amount to withdraw: "); //Only executed if userInput == withdraw
amount = in.nextDouble(); //Always executed
if(userInput.equals(withdraw)) {
System.out.println("Enter amount to withdraw: ");
amount = in.nextDouble();
//All executed
}
这样做:
if(userInput.equals(withdraw)) {
System.out.println("Enter amount to withdraw: ");
amount = in.nextDouble();
if(amount > currentBalance)
System.out.println("You have exceeded your amount.");
debit(currentBalance, amount);
System.out.println("Your new balance is: " + currentBalance);
}
if (userInput.equals(deposit)) {
System.out.println("Enter amount to deposit: ");
amount = in.nextDouble();
credit(currentBalance, amount);
System.out.println("Your new balance is: " + currentBalance);
}
请注意,如果您提取的金额超过当前余额,您将收到“警告消息”,但您的提款将继续。因此,你最终会得到一笔负数。如果您不想这样做,则必须相应地进行更改。但是,这种方式表明括号(或不使用括号)的使用有不同的效果。
答案 2 :(得分:2)
if (userInput == deposit)
应该是
if (userInput.equals(deposit))
退出相同。
答案 3 :(得分:2)
关于这些方法:
public static double debit(double currentBalance, double amount) {
currentBalance -= amount;
return currentBalance;
}
public static double credit(double currentBalance, double amount) {
currentBalance += amount;
return currentBalance;
}
函数的输入实际上不应该包括当前的平衡,对象已经知道当前的平衡是什么(它被保存在对象currentBalance字段中,正如已经指出的那样不应该是静态的)。
想象一下真实的自动提款机,表现如下:
Whats my current balance:
£100
CreditAccount("I promise my current balance is £1 Million, it really is", £10):
Balance:£1,000,010
编辑:包含与此类似的代码
import java.util.*;
public class Account {
private int accountNumber;
private double currentBalance; //balance kept track of internally
// ***** CONSTRUCTOR *****//
public Account(int accountNumber, double currentBalance) {
this.accountNumber = accountNumber;
this.currentBalance = currentBalance;
}
public int getAccountNumber() {
return accountNumber;
}
public double getcurrentBalance() {
return currentBalance;
}
public boolean debit(double amount) {
//we just refer to the objects fields and they are changed
if (currentBalance<amount){
return false; //transaction rejected
}else{
currentBalance -= amount;
return true;
//transaction approaved and occured
}
//Note how I directly change currentBalance, there is no need to have it as either an input or an output
}
public void credit( double amount) {
//credits will always go through, no need for return boolean
currentBalance += amount;
//Note how I directly change currentBalance, there is no need to have it as either an input or an output
}
public static void main(String [] args){
Account acc=new Account(1234,1000);
acc.credit(100);
System.out.println("Current ballance is " + acc.getcurrentBalance());
boolean success=acc.debit(900); //there is enough funds, will succeed
System.out.println("Current ballance is " + acc.getcurrentBalance());
System.out.println("Transaction succeeded: " + success);
success=acc.debit(900); //will fail as not enough funds
System.out.println("Current ballance is " + acc.getcurrentBalance());
System.out.println("Transaction succeeded: " + success);
}
}
我没有打扰使用输入的输入,因为你似乎已经挂了那个
答案 4 :(得分:1)
如果没有'{'和'}',if语句后面的第一行只会作为该语句的一部分执行。此外,您的if (userInput == deposit)
块未正确缩进,不应位于if (userInput == withdraw)
下。字符串比较应使用userInput.equals(withdraw)
答案 5 :(得分:1)
使用equals()方法代替==比较对象的相等而不是值
import java.util.*;
public class Account{
private int accountNumber;
private static double currentBalance;
private static double debit;
// ***** CONSTRUCTOR *****//
public Account(double currentBalance, int accountNumber) {
accountNumber = 12345;
currentBalance = 10000.00;
}
public int getAccountNumber(int accountNumber) {
this.accountNumber = accountNumber;
return accountNumber;
}
public double getcurrentBalance(double currentBalance) {
this.currentBalance = currentBalance;
return currentBalance;
}
public static double debit(double currentBalance, double amount) {
currentBalance -= amount;
return currentBalance;
}
public static double credit(double currentBalance, double amount) {
currentBalance += amount;
return currentBalance;
}
public static void main(String [] args){
String withdraw = "Withdraw";
String deposit = "Deposit";
double amount;
Scanner in = new Scanner(System.in);
System.out.println("Are you withdrawing or depositing? ");
String userInput = in.nextLine();
if(userInput.equals(withdraw))
System.out.println("Enter amount to withdraw: ");
amount = in.nextDouble();
if(amount > currentBalance)
System.out.println("You have exceeded your amount.");
debit(currentBalance, amount);
System.out.println("Your new balance is: " + currentBalance);
if (userInput .equals(deposit))
System.out.println("Enter amount to deposit: ");
amount = in.nextDouble();
credit(currentBalance, amount);
System.out.println("Your new balance is: " + currentBalance);
}
}
答案 6 :(得分:1)
借记和贷记方式:
public static boolean debit(double currentBalance, double amount) {
currentBalance -= amount;
if<currentBalance < 0){
return false
}
return true;
}
public static boolean credit(double currentBalance, double amount) {
currentBalance += amount;
if<currentBalance > 0){
return false
}
return true;
}
现在我觉得我把布尔值混淆了。对于每种方法的回报,描述有点不清楚。