2014-11-24 38 views
-4

我的程序應該要求用戶輸入他們想從他們的賬戶中提取的金額,並在退出後計算當前餘額。 提款要求最低100,最高1000.如果用戶輸入錯誤的輸入,程序應重新提示並要求用戶再次輸入金額。這個過程將不斷重複,直到用戶投入適量。 一旦選擇了正確的金額,它應該計算並顯示當前餘額。如何爲用戶輸入創建循環(直到用戶輸入有效的輸入)?

這是我試過,但我沒有做循環:

package ex3; 

    import java.util.Scanner; 

    public class BankApp { 

     public static void main(String[] args) { 
      //displaying the welcome message 
      System.out.println("Welcome to our bank.\nYour initial balance is 1000 SEK\n"); 
      //initializing all necessary variables 
      double initialBalance = 1000; 
      double userChoise = 0; 
      double currentBalance; 

      //asking user to enter expected amount 
      System.out.println("Enter your amount you want to withdraw (in SEK): "); 
      //creating new instance of the scanner class 
      Scanner iScanner = new Scanner(System.in); 
      //store into userChoise whatever amount is chosen by user 
      userChoise = iScanner.nextDouble(); 
      //checking wheather the user inputs any valid amount or not. In this case it must be minimum 100 and maximum 1000. 
      if(userChoise < 100 || userChoise > 1000) 
      { 
       System.out.println("Error: Enter your amount again(in SEK): "); 
      } 
      else { 
       currentBalance = initialBalance - userChoise; 
       System.out.printf("You have withdrawn %.2f\n", userChoise); 
       System.out.printf("Your current balance is %.2f\n", currentBalance); 
      } 
     } 
    } 
+1

您發佈的代碼中沒有循環語句。我沒有看到你「嘗試但沒有成功」的地方。 – ajb 2014-11-24 17:34:33

+2

這裏有一個提示 - 你有問題用while循環標記,但我沒有看到代碼中的while循環。 – user2366842 2014-11-24 17:34:52

+0

'notdone = false; while(notdone){提示輸入}' – 2014-11-24 17:35:35

回答

0

使用,而與真實情況循環,當你想打破打破。

package ex3; 

     import java.util.Scanner; 

     public class BankApp { 

      public static void main(String[] args) { 
       //displaying the welcome message 
       System.out.println("Welcome to our bank.\nYour initial balance is 1000 SEK\n"); 
       //initializing all necessary variables 
       double initialBalance = 1000; 
       double userChoise = 0; 
       double currentBalance; 

       //asking user to enter expected amount 
       System.out.println("Enter your amount you want to withdraw (in SEK): "); 

       //creating new instance of the scanner class 
       Scanner iScanner = new Scanner(System.in); 

       while(true){ 

       //store into userChoise whatever amount is chosen by user 
       userChoise = iScanner.nextDouble(); 
       //checking wheather the user inputs any valid amount or not. In this case it must be minimum 100 and maximum 1000. 
       if(userChoise < 100 || userChoise > 1000) 
       { 
        System.out.println("Error: Enter your amount again(in SEK): "); 
       } 
       else { 
        currentBalance = initialBalance - userChoise; 
        System.out.printf("You have withdrawn %.2f\n", userChoise); 
        System.out.printf("Your current balance is %.2f\n", currentBalance); 
        break; 
       } 
      } 
      } 
     } 
+1

非常感謝,它工作正常。我會在幾分鐘內接受你的回答。 – Ryan 2014-11-24 17:42:53

+0

歡迎您:) – 2014-11-24 17:45:23

相關問題