2014-09-03 25 views
0

你好下面是我Progarm如何基於Java中使用掃描儀的用戶選擇重複循環

import java.util.Scanner; 

public class Usecase1 { 
    public static void main(String[] args) { 
     Usecase1 us = new Usecase1(); 
     us.withdrawl(); 

    } 

public void withdrawl() { 
    System.out.println("your Account number is....0091236452312"); 
    System.out.println("please enter your pin number(1234)...."); 
    Scanner sc = new Scanner(System.in); 

    int pinno = sc.nextInt(); 
    if (pinno == 1234) { 
     System.out 
       .println("Please select type of Transaction 1.Balance Enquiry 2.Withdraw Money"); 
     int option = sc.nextInt(); 
     System.out.println("your choice is..." + option); 
     int totalamount = 85000; 
     if (option == 2) { 
      System.out.println("enter amount to withdraw"); 
      int amount = sc.nextInt(); 
      System.out.println("Remaining Balance in your account is..." 
        + (totalamount - amount)); 
     } 

    } 
} 
} 

我的要求是,用戶可以給自己的選擇,因爲2任意次數,每一個下面的循環應時間請重複,請幫助我如何做到這一點。

(option == 2) { 
       System.out.println("enter amount to withdraw"); 
       int amount = sc.nextInt(); 
       System.out.println("Remaining Balance in your account is..." 
         + (totalamount - amount)); 
      } 
+0

使用'while循環'while(true){if(option == 2){//當沒有更多動作需要時執行s break和break}} – 2014-09-03 09:18:17

+0

當選項中有'continue' == 2 – 2014-09-03 09:21:39

回答

0

使用while環爲和boolean來標記循環

boolean isValid = true; 
int totalamount = 85000; 
while(isValid){ 
    System.out.println("Please select type of Transaction 1.Balance Enquiry 2.Withdraw Money 3.Exit"); 
     int option = sc.nextInt(); 
     System.out.println("your choice is..." + option); 
     if (option == 2) { 
      System.out.println("enter amount to withdraw"); 
      int amount = sc.nextInt(); 
      System.out.println("Remaining Balance in your account is..." 
        + (totalamount - amount)); 
     } else if(option == 3){ 
      //exit 
      isValid = false; 
     } 
     System.out.println("Do you want to continue? 1.yes 2.no"); 

     int lastPrompt = sc.nextInt(); 

     if(lastPrompt == 2){ 
     break; or isValid = false; 
     } 
} 
+0

感謝它的工作,最後的懷疑....在重複之前,我們需要t o顯示一條消息,說你想繼續ID用戶說是的只有循環應該重複,請告訴我如何做到這一點 – user3824049 2014-09-03 09:25:56

+0

@ user3824049我編輯了我的答案 – 2014-09-03 09:28:16

+0

感謝它的工作 – user3824049 2014-09-03 09:31:50

0

使用while環和 使用breakcontinue基於對你的條件循環控制。
EG-

while(true){ 
    if (option == 2) { 
        System.out.println("enter amount to withdraw"); 
        int amount = sc.nextInt(); 
        System.out.println("Remaining Balance in your account is..." 
          + (totalamount - amount)); 
       } 
    else{ 
      break;//break the loop 
      //or you may continue the loop 
     } 
    } 
0

更換,如果(選項== 2)while循環:

while (option == 2) { 
     System.out.println("enter amount to withdraw"); 
     int amount = sc.nextInt(); 
     System.out.println("Remaining Balance in your account is..." + (totalamount - amount)); 

     //Stay in the loop if option is 2 again  
     option = sc.nextInt(); 
    } 

但是,如果你要考慮更多的選項,然後用標誌像在下面的答案