2014-09-03 44 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(); 
    } 

但是,如果你要考虑更多的选项,然后用标志像在下面的答案