Java Bank Program有一个奇怪的幽灵循环?

时间:2011-10-26 03:34:20

标签: java

我无法解决这个问题。程序中的所有内容都按我想要的方式工作,除非我使用'o'开帐户选项,'w'撤销accout选项等等,在完成后,根据需要重复循环,但返回else行“Command is不承认;请再试一次。“然后重新打印菜单。如果我使用's'命令它工作正常。我删除了“balance = input.nextDouble();”行,它工作正常,但我无法弄清楚为什么它返回else语句和重印。

import java.util.Scanner;
import java.text.*;

public class Bank
{
     public static void main( String[] args )
     {
         Scanner input = new Scanner( System.in );
            String eol = System.getProperty("line.separator");

            DecimalFormat df = new DecimalFormat("0.00");

            int numAccounts = 1;
            String number = "None Selected";
            String userInput;
            double balance = 0;
            double amount = 0;
            int i = 0;
            int j;

            BankAccount[] accounts = new BankAccount [100];

            accounts[0] = new BankAccount (number, balance); 

            String BBalance = df.format(accounts[i].getBalance());

            while (1 < 2){

                if (numAccounts == accounts.length){
                    BankAccount[] tempArray = new BankAccount[accounts.length * 2];
                            for (int k = 0; k < accounts.length; k++){
                                tempArray[k] = accounts[k];
                                }
                                accounts = tempArray;

                }
                try{
                    System.out.print ( eol +
                                    "-----------------------------------------------------" + eol +
                                    "|Commands: o - Open account        c - Close account|" + eol +
                                    "|          d - Deposit             w - Withdraw     |" + eol +
                                    "|          s - Select account      q - Quit         |" + eol +
                                    "-----------------------------------------------------" + eol +
                                    "Current account: " + accounts[i].getNumber() + "       Balance: $" + df.format(accounts[i].getBalance()) +
                                    eol );
                    }
                catch(java.lang.Throwable t) {
                System.out.println("Account number was not found");
                }

                userInput = input.nextLine().trim().toLowerCase();


                if (userInput.substring(0).equals("o")) {
                    System.out.print("Enter Account Number: ");
                    number = input.nextLine();
                    System.out.print("Enter Initial Balance: ");
                    balance = input.nextDouble();

                    accounts[numAccounts++] = new BankAccount (number, balance); 
                    i = numAccounts - 1;
                    continue;
                }
                else if (userInput.substring(0).equals("d")) {
                    System.out.print("Enter Account Number: ");
                    number = input.nextLine();
                        for (i =0; i < numAccounts; i++)
                            if (accounts[i].getNumber().equals(number))
                                break;


                    System.out.print("Enter Amount To Deposit: ");
                    amount = input.nextDouble();

                    accounts[i].deposit(amount);
                    continue;
                }
                else if (userInput.substring(0).equals("s")) {
                    System.out.print("Enter Account Number: ");
                    number = input.nextLine();
                        for (i =0; i < numAccounts; i++){
                            if (accounts[i].getNumber().equals(number)){
                                break;
                            }

                        }
                        if (i != numAccounts){
                            System.out.print("Account number was not found");
                        }
                        continue;
                }
                else if (userInput.substring(0).equals("c")) {
                    System.out.print("Enter Account Number: ");
                    number = input.nextLine();
                        for (i =0; i < numAccounts; i++)
                            if (accounts[i].getNumber().equals(number))
                                break;

                    accounts[i] = accounts[--numAccounts];
                    continue;
                }
                else if (userInput.substring(0).equals("w")) {
                    System.out.print("Enter Account Number: ");
                    number = input.nextLine();
                        for (i =0; i < numAccounts; i++)
                            if (accounts[i].getNumber().equals(number))
                                break;


                    System.out.print("Enter Amount To Withdraw: ");
                    amount = input.nextDouble();

                    accounts[i].withdraw(amount);
                    continue;
                }
                else if (userInput.substring(0).equals("q")) {

                    System.exit(0);
                }
                else{
                    System.out.println("Command was not recognized; please try again.");
                    continue;
                }

            }

            //System.out.print(userInput);
            //System.out.print(accounts[0]);

     }
}

这是BankAccount类:

public class BankAccount {

    String number = "123-456";
    double balance = 1.00; 

    public BankAccount(String accountNumber, double initialBalance){
        number = accountNumber;
        balance = initialBalance;
    }
    public void deposit (double amount){
        balance += amount;
    }
    public void withdraw (double amount){
        balance -= amount;
    }
    public String getNumber(){
        return number;
    }
    public double getBalance(){
        return balance;
    }

    BankAccount[] accounts = new BankAccount [100];

}

1 个答案:

答案 0 :(得分:0)

当您在需要一个命令的第二个数字中读取时,您不会跳过行尾。

为这些添加input.nextLine()是一种快速解决方法(非I / O行编辑)。

    // etc.
} else if (userInput.substring(0).equals("d")) {
    System.out.print("Enter Account Number: ");
    number = input.nextLine();

    System.out.print("Enter Amount To Deposit: ");
    amount = input.nextDouble();
    input.nextLine();

    continue;
} // etc.