2014-10-06 36 views
1

這是在Java中,我的問題是,當用戶輸入「depositAmount」程序崩潰,如果他們使用小數點或逗號,我明白,他們的輸入需要是雙重類型,但我不知道如何說:「如果用戶的輸入是雙,接受它,否則,要求一個新的輸入」如何處理我的代碼以防止我的程序在用戶輸入小數點時崩潰?

import java.util.Scanner; 
import java.util.ArrayList; 
public class Engine 

{ 

private static ArrayList<BankAccount> accounts = new ArrayList<BankAccount>(); 

public static void Engine() 
{ 

    Scanner Reader = new Scanner(System.in); 
    BankAccount n = new BankAccount(); 
    boolean keepGoing = true; 

    while(keepGoing) 
    { 
     System.out.println("Welcome to The Bank of Money, what would you like to do?\n enter n to create a new account, enter e to use an existing account, or enter q to quit to main menu"); 
     String response = Reader.nextLine(); 
     if(response.equals("q")) keepGoing = false; 
     else if(response.equals("n")) 
     { 
      accounts.add(new BankAccount()); 
      System.out.println("Your account number is: " + accounts.get(accounts.size()-1).getAccountNum()); 
     } 
     else if(response.equals("e")) 
     { 
      System.out.println("what is your account number?"); 
      Scanner Reader1 = new Scanner (System.in); 
      int accountNum = Reader1.nextInt(); 
      BankAccount selectedAccount = null; 
      for(int i = 0; i<accounts.size();i++) 
      { 
       if (accounts.get(i).getAccountNum()==accountNum) 
       { 
        selectedAccount = accounts.get(i); 

       } 
      } 
      if(selectedAccount ==null)System.out.println("There is no such account. Please try again."); 
      else transact(selectedAccount); 
     } 
    } 
} 

public static void transact(BankAccount selectedAccount) 
{ 
    Scanner in = new Scanner(System.in); 
    int response = 0; 
    while(response!=7) 
    { 
     BankAccount.printTransactionMenu(); 
     response = in.nextInt(); 
     if(response==1) 
     { 
      System.out.println("Enter the amount that you would like to deposit"); 
      System.out.print("$"); 
      Scanner depositScanner = new Scanner(System.in); 
      double depositAmount = depositScanner.nextInt(); 
     } 
     if(response==2) 
     { 
      System.out.println("Enter the amount that you would like to withdraw"); 
      System.out.print("$"); 
      Scanner withdrawScanner = new Scanner(System.in); 
      double withdrawAmount = withdrawScanner.nextInt(); 
      if(withdrawAmount>selectedAccount.getBalance()) 
       System.out.println("Insufficient funds"); 
      else selectedAccount.withdraw(withdrawAmount); 
     } 
     if(response==3) 
     { 
      selectedAccount.assessInterest(); 
      System.out.println("Your new balance is $" + selectedAccount.getBalance()); 
     } 
     if(response==4) 
     { 
      System.out.println("Your account balance is $" + selectedAccount.getBalance()); 
     } 
     if(response==5) 
     { 
      System.out.println("Which account would you like to transact with?"); 
      Scanner Reader1 = new Scanner (System.in); 
      int transactAccountNum = Reader1.nextInt(); 
      BankAccount transactAccount = null; 
      for(int i = 0; i<accounts.size();i++) 
      { 
       if (accounts.get(i).getAccountNum()==transactAccountNum) 
       { 
        transactAccount = accounts.get(i); 
       } 
      } 
      if(transactAccount ==null)System.out.println("There is no such account. Please try again."); 
      else 
      { 
       System.out.println("What amount would you like to transfer?"); 

       Scanner transferScanner = new Scanner(System.in); 
       double transactAmount = transferScanner.nextDouble(); 
       selectedAccount.transfer(transactAccount, transactAmount); 
      } 
     } 
     if(response==6) 
     { 
      selectedAccount.printStatement(); 
     } 
    } 
} 

}

+0

使用['nextDouble()'](http://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html#nextDouble--)而不是'nextInt()'。另外,還有一個'hasNextDouble()'方法,您可以使用它來事先檢查輸入中是否實際存在'double'。 – ajb 2014-10-06 22:43:45

回答

1

您可以從掃描儀輸入不最初有一個int或double值,使用next()方法。如果你知道數據類型,你可以檢查它是否有下一個double(hasNextDouble)或你需要的任何數據類型。如果沒有,您可以採用字符串類型並將其轉換。

Scanner in = new Scanner(System.in); 
String s = in.next(); 

從那裏你可以把它解析成適當的數據類型(例如double)。

double d = Double.parseDouble(s); 

我希望這有助於!

0

您可以在java中輕鬆使用try/catch段來達到此目的。 如果用戶輸入了一個整數而不是十進制,程序會產生一個Cast Exception,然後您可以按照您的意願處理它(例如要求用戶再次輸入)。

相關問題