如何在java中使用scanner重复基于用户选择的循环

如何在java中使用scanner重复基于用户选择的循环,java,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..

嗨,下面是我的程序

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));
            }

使用
while
循环进行该操作,并使用
布尔值来标记循环

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;
         }
}

使用
while
循环和 使用
break
continue
根据您的情况控制循环。
乙二醇-

用while循环替换if(选项==2):

    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();
    }

<如果你想考虑更多的选项,然后使用一个在下面的答案中使用的标志:使用<代码> while循环<代码> > <代码> >(true){if(选项=2){//当没有更多的动作需要} //dos&休息时}<代码> >你有一个更多的选项使用<代码>继续<代码>当选项=2时谢谢它的工作,最后一个疑问…在重复之前,我们需要显示一条消息,说明您是否要继续id用户说是,只应重复循环,请告诉我如何执行此操作
    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();
    }