可以再次使用方法

时间:2014-11-02 19:24:09

标签: java eclipse

我在循环程序时遇到问题,以便在用户退出之前继续运行。

用户选择一个选项后,它会再次显示我放入的时间菜单,但它不会再次进入其他方法,只是再次显示菜单。我认为我的问题是选择不是"重置"并且程序已经认为用户输入了????

提前感谢您的帮助! (对不起,我把整个程序放在这里,我不确定需要什么以及解决这个问题的原因。)

 /*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. 
}

}

1 个答案:

答案 0 :(得分:1)

两个问题。

(1)你做了一个简单的拼写错误。这条线

 while (input>=0 || input<=5)

应该说

while (input <= 0 || input >= 5)

因为您希望循环在input的值无效时继续运行。

(2)这部分

while (choice==1 || choice==2 ||choice==3)
{
    choice=menu();
}   

是完全错误的,因为你最终会收集输入而不是采取行动。您需要将大switch/case语句放在此循环中,而不是在外部。