gpt4 book ai didi

Java ATM 程序

转载 作者:行者123 更新时间:2023-11-30 03:41:45 25 4
gpt4 key购买 nike

我正在做一个自动取款机程序,我很难弄清楚如何让它实际存款和取款。余额一开始自动为 0 美元,但我无法输入任何内容来实际添加或减去它,我做错了什么?

public class ATM {
static Scanner keyboard = new Scanner(System.in);
static String acctNum, pwd, result;
static double oldBalance, newBalance, deposit, withdraw;
static int choose;

public static void main(String[] args) {
for (int run = 0; run < 3; run++) {
System.out.println("Enter your account number");
acctNum = keyboard.nextLine();
System.out.println("Enter your account password");
pwd = keyboard.nextLine();

result = checkID(acctNum, pwd);
if (!result.equals("ERROR")) {
break;
} else if (run == 2) {// you cannot try to log in anymore than 3
// times
System.out.println("MAXIMUM TRIES EXCEEDED");
return;
}

}
menu();
}

public static String checkID(String acctNum, Object pwd) {
String result = "ERROR";
String a = "44567-5 mypassword 520.36";
String b = "1234567-6 anotherpassword 48.20";
String c = "4321-0 betterpassword 96.74";

if (acctNum.equals("44567-5") && pwd.equals("mypassword")) {
result = "520.36";
} else if (acctNum.equals("1234567-6") && pwd.equals("anotherpassword")) {
result = "48.20";
} else if (acctNum.equals("4321-0") && pwd.equals("betterpassword")) {
result = "96.74";
}
System.out.println(result);
return result;
}

public static int menu() {
System.out
.println("Choose one of the following: \n1.Display Balance\n2.Deposit\n3.Withdraw\n4.Log Out");
choose = keyboard.nextInt();

if (choose == 1) {// 1. Display Balance
displayBalance();
menu();
return 1;

}
if (choose == 2) {// 2. Deposit
deposit();
menu();
return 2;

}
if (choose == 3) {// 3. Withdraw
withdraw();
menu();
return 3;

}
if (choose == 4) {// 4. Log out
System.out.println("You are logged out.");
return 4;

}
if (choose <= 5) {// type in anything greater than 4 and you will get a
// system error
System.out.println("System Error");
menu();
return 5;
}
if (choose >= 1) {// type in anything less than 1 and you will get a
// system error
System.out.println("System Error");
menu();
return 6;
}
return choose;

}

public static double deposit() {
System.out.println("How much would you like to deposit?");
deposit = keyboard.nextInt();
System.out.println((deposit + oldBalance)==newBalance);// deposit money into balance
return 2;
}

public static double displayBalance() {
System.out.println("Total balance is: $" + oldBalance);
oldBalance = 0.00;
return 1;
}

public static double withdraw() {
System.out.println("How much would you like to withdraw?");
withdraw = keyboard.nextInt();
System.out.println(newBalance + withdraw);// withdraw money from balance
return 3;
}

}

最佳答案

一些观察:

  1. 您的类不应将 oldBalance、存款和取款声明为全局变量。提款方法不应访问存款变量,反之亦然。尽可能限制变量的范围是一种很好的编程习惯。因为此方法之外实际上并不需要“deposit”变量,所以绝对没有理由将其设为全局变量。对于“当前余额”则不能这样说。类的所有方法都需要此变量:存款、取款、displayBalance 等。因此,有充分的理由将其设为全局(不过,如果您可以用较小的范围解决问题,您应该这样做)。

  2. 我不明白为什么 deposit()withdrawal() 需要返回任何内容。

deposit()withdrawal() 最简单的实现是将用户输入添加到 currentBalance 并将其保存在当前余额

public void deposit()
{
Scanner input = new Scanner(System.in);
System.out.printn("Enter deposit amount: );
double amount = input.nextDouble();
System.out.println("Your deposit amount: " + amount);
currentBalance += amount;
System.out.println("Your new balance is: + currentBalance);
}

public void withdrawal()
{
Scanner input = new Scanner(System.in);
System.out.printn("Enter withdrawal amount: );
double amount = input.nextDouble();
System.out.println("Your withdrawal amount: " + amount);
currentBalance -= amount;
System.out.println("Your new balance is: + currentBalance);
}

您永远不会保存当前余额。 == 运算符是比较运算符,而不是赋值运算符。当你做这样的事情时:

System.out.println((deposit + oldBalance)==newBalance);

或者像这样:

System.out.println(newBalance + withdraw);

您所做的只是将值传递给 println() 方法,而不是更新应该保持平衡的变量。事实上,第一个应该打印 false (除非 Deposit 和 oldBalance 都为零)。如果您查看我的代码示例,currentBalance += amount;currentBalance = currentBalance + amount; 的简写,意思是“将此 amount 添加到currentBalance 并将其存储在 currentBalance 中。因为我重用了 currentBalance,所以不再需要 oldBalance 变量。

关于Java ATM 程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26706313/

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