我无法弄清楚如何让我的main方法中的double balance1等于我的menu方法中的double acctBal。我将0分配给了acctBal,因为我不确定如何将它等同于任何balance1。我在我说的那条线旁边放了一些星号。
公共班级ATM {
public static Scanner kbd;
public static void main(String[] args) {
String acctNum, acctPword;
String balance;
int x = 1;
kbd = new Scanner(System.in);
System.out.print("Enter your account number: ");
acctNum = kbd.nextLine();
System.out.print("Enter your account password: ");
acctPword = kbd.nextLine();
balance = checkID(acctNum, acctPword);
double balance1 = Double.parseDouble(balance);
System.out.println("You currently have $" + String.format("%.2f",balance1));*********************************
while (balance.equals("error") && x <4){
System.out.println("Wrong password try again.");
System.out.print("Enter your account password: ");
acctPword = kbd.nextLine();
x++;
}
if (x == 4)
System.out.println("Maximum number of attempts reached, Please try again later.");
if (x == 1){
menu();
}
kbd.close();
}
/**
* Determines if acctNum is a valid account number, and pwd is the correct
* password for the account.
* @param acctNum The account number to be checked
* @param pwd The password to be checked
* @return If the account information is valid, returns the current account
* balance, as a string. If the account information is invalid, returns
* the string "error".
*/
public static String checkID(String acctNum, String pwd)
{
String result = "error";
// Strings a, b, and c contain the valid account numbers and passwords.
// For each string, the account number is listed first, followed by
// a space, followed by the password for the account, followed by a space,
// followed by the current balance.
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";
return result;
}
if (acctNum.equals("1234567-6") && pwd.equals("anotherpassword")){
result = "48.20";
return result;
}
if (acctNum.equals("4321-0") && pwd.equals("betterpassword")){
result = "96.74";
return result;
}
return result;
}
public static void menu(){
int x = 0;
double acctBal = 0.0, deposit = 0.0, withAmnt = 0.0;**********this acctBal needs to equal balance1.
System.out.println("1. Display Balance \n2. Deposit\n3. Withdraw\n4. Log Out");
x = kbd.nextInt();
switch(x){
case 1:
displayBalance(acctBal);
break;
case 2:
deposit(acctBal, deposit);
break;
case 3:
withdraw(acctBal, withAmnt);
break;
case 4:
System.out.println("Have a nice day.");
break;
}
}
public static double deposit(double acctBal, double depAmnt){
System.out.print("How much money would you like to deposit? $");
depAmnt = kbd.nextDouble();
acctBal = acctBal + depAmnt;
System.out.println("Your balance is now at $" + String.format("%.2f", acctBal));
return acctBal;
}
public static double withdraw(double acctBal, double withAmnt){
System.out.print("How much money would you like to withdraw? $");
withAmnt = kbd.nextDouble();
if (acctBal <= 0){
System.out.println("You do not have any money.");
return acctBal;
}
if (acctBal < withAmnt){
System.out.print("You do not have enough money.\nHow much money would you like to withdraw? $");
withAmnt = kbd.nextDouble();
}
else{
acctBal = acctBal - withAmnt;
}
return acctBal;
}
public static double displayBalance(double balance){
System.out.println("Your balance is $" + String.format("%.2f", balance));
return balance;
}
答案 0 :(得分:0)
您可以更改menu()
方法以接受参数(称为balance
)并在调用main
方法时传递menu()
的值,例如:
public static void menu(double acctBal){
double deposit = 0.0; //Other variables
}
并使用main
从balance
拨打电话,例如:
if (x == 1){
menu(balance);
}
答案 1 :(得分:0)
您的代码有问题,您不会处理checkID
返回&#34;错误&#34;的情况。你不能分配&#34;错误&#34;到一些String,然后尝试从该String转换一个double。
次要修复
balance = checkID(acctNum, acctPword);
double balance1 = 0.0;
if (balance.equals("error"))
{
// TODO: do something with invalid number.
}
else
{
balance1 = Double.parseDouble(balance);
}
System.out.println("You currently have $" + String.format("%.2f",balance1));