2011-10-26 38 views
0

我無法解決這個問題。除了我使用'o'開放賬戶選項,'w'撤銷賬戶選項..等完成後,按照需要重複循環,但返回else行「Command is was未被識別,請重試。「然後重新打印菜單。如果我使用's'命令,它工作正常。我刪除了「balance = input.nextDouble();」行,它工作正常,但我不明白爲什麼它返回else語句和重印。Java銀行計劃有一個奇怪的幽靈循環?

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

OT無限循環通常只表示'while(true)'。 –

+2

而1 <2更加令人興奮。誰知道,也許有一天事情會改變...... –

回答

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. 
+0

我讚賞它戴夫。修復它。 –

+0

@NadiaWood然後[接受答案](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work);) –