我在循環我的程序時遇到問題,因此它會一直持續到用戶退出。無法使用方法agian
在用戶選擇一個選項後,它會在我放置的時候再次顯示菜單,但它不會再次進入其他方法,它只會再次顯示菜單。我認爲我的問題是,選擇不是「重置」,程序已經認爲用戶有輸入?
在此先感謝您的幫助! (對不起,我把整個程序放在這裏,我不確定需要什麼,什麼不能解決這個問題)。
/*This program simulates a simple ATM machine.
User can withdraw and deposit money by entering correct acct number and password.*/
import java.util.*;
public class ATM {
public static Scanner kbd = new Scanner(System.in);
public static void main(String[] args)
{
String acctNum, pwd, verifyID;
int choice, attempts=0;
double acctBal, depositAmount, withdrawAmount;
System.out.println("Enter account number: ");
acctNum=kbd.next();
System.out.println("Enter password: ");
pwd=kbd.next();
verifyID=checkID(acctNum, pwd);
//calls checkID method and returns result to verify
while(verifyID.equals("error") && attempts<=3)
//checkID returns "error" and the attempts are less than or equal to 3.
{
attempts++;
System.out.println("Entered the incorrect account information please try again.\n");
//verifyID=checkID(acctNum, pwd);
if (attempts==4) //If Maximum attempts reached display message, then exit.
{
System.out.print("Maxium attemps reached. Your session has ended.");
System.exit(1);
}
else
{
System.out.println("Re-enter password: ");
pwd=kbd.next();
//verifyID=checkID(acctNum, pwd);
}
}
acctBal=Double.parseDouble(verifyID);
//Changing the string from checkID method to double *(acct balance is returned)
choice=menu();//Calls menu() method.
switch (choice)
{
case(1)://user enters balance option.
displayBalance(acctBal); //Call method displayBalance.
break;
case(2)://User enters deposit option.
System.out.println("Enter deposit amount: ");
depositAmount=kbd.nextDouble();
acctBal=deposit(acctBal,depositAmount);//call method deposit().
System.out.printf("Your new current balance is:$ %.2f\n", acctBal); //Print new acctBal to screen.
break;
case (3)://User enters withdraw option.
System.out.println("Enter amout to withdraw: ");
withdrawAmount=kbd.nextDouble();
acctBal=withdraw(acctBal, withdrawAmount);//call method withdraw().
System.out.printf("Your new current balance is:$ %.2f\n", acctBal); //Print new acctBal to screen.
break;
case(4)://User enters log-out
System.out.println("You have been logged out.");
System.exit(1);
}
while (choice==1 || choice==2 ||choice==3)
{
choice=menu();
}
}
public static void displayBalance(double acctBal)
{
System.out.printf("Your current balance is :$ %.2f\n", acctBal); //Displays current balance.
}
/*The checkID method determines if acctNum is a valid account number
and pwd is the correct password for the account. If the account information
is valid, the method returns the current account balance, as a string.
If the account information is invalid, the method 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(a.substring(0, a.indexOf(" "))) &&
pwd.equals(a.substring(a.indexOf(" ")+1, a.lastIndexOf(" "))))
{
result=a.substring(a.lastIndexOf(" ") +1);
}
else if (acctNum.equals(b.substring(0, b.indexOf(" "))) &&
pwd.equals(b.substring(b.indexOf(" ")+1, b.lastIndexOf(" "))))
{
result=b.substring(b.lastIndexOf(" ") +1);
}
else if (acctNum.equals(c.substring(0, c.indexOf(" "))) &&
pwd.equals(c.substring(c.indexOf(" ")+1, c.lastIndexOf(" "))))
{
result=c.substring(c.lastIndexOf(" ") +1);
}
return result;
// insert code here to determine if acctNum is a valid account number
// and pwd is the correct password for the account.
}
public static double deposit(double acctBal, double depositAmount)
{
return acctBal=acctBal + depositAmount;
}
public static double withdraw(double acctBal, double withdrawAmount)
{
if (acctBal<=withdrawAmount)
{
System.out.println("Insuffienct funds.");
return acctBal;
}
else
{
return acctBal-withdrawAmount;
}
}
public static int menu()
{
int input=0;
while (input>=0 || input<=5) //check side
//Checking input from user is between 1-4.
{
System.out.println("\nMain Menu\n1. Display Balance\n\n2. Deposit\n\n"
+ "3. Withdraw\n\n4. Log Out\n\n(Please enter 1, 2, 3, or 4):");
input=kbd.nextInt();
if (input<=0 ||input>=5)
//If input is not between 1-4 print error.
{
System.out.println("Invaid input.");
}
else
{
return input; //Calls whichever method the user selected.
}
}
return input; //Calls whichever method the user selected.
}
}
請你指點一下讓你煩惱的while循環嗎? – 2014-11-02 19:28:04
'input> = 0 ||輸入<= 5'總是如此。使用'input> = 0 && input <= 5'來使用邏輯**和**。 – Tom 2014-11-02 19:28:05
@Tom,我放棄了我之前對你的評論作出正確回答的評論。它應該是'input <= 0 ||輸入> = 5'。無需將'||'更改爲'&&',因爲Harbinger希望循環在輸入無效時保持重複,而不是在有效時。 – 2014-11-02 19:33:10