简体   繁体   中英

by pass do while loop

I wondering is it possible to skip past a do while loop or even exit a method halfway through it if certain conditions are met? Heres an example of what I mean ( everything works in this )

  public void loanBook() {
            Scanner input = new Scanner(System.in);
            boolean successful = false;
            boolean bookExists = false;
            boolean memberIDExists = false;
            boolean memberCurrentlyLoaningBook = false;
            int memberID = 0;
            System.out.println("You must be a member of the library to loan out a book");
            do {
                System.out.println("\nPlease enter your memberID (Press 9 to exit)");
int bookID = input.nextInt();
                input.nextLine();
                if (bookID == 9) {
                    successful = true;
                }
                memberID = input.nextInt();
                for (int i = 0; i < members.size(); i++) {
                    if (memberID == members.get(i).getMemberID()) {
                        memberIDExists=true;
                        if(members.get(i).isCurrentlyLoaningBook()==false){
                            successful = true;
                            memberCurrentlyLoaningBook=true;
                        }
                    } 
                }

                if(!memberCurrentlyLoaningBook){
                    System.out.println("This member is currently loaning a book");
                }

                if (!memberIDExists) {
                    System.out.println("This member ID does not exist");
                }

            } while (successful == false);

EDIT do while loops that are in the same method

do { these other do while loops are after this first one and do other things } while (); ( I haven't included what they do as it takes up too do { much space) } while();

I have an option here to exit to the main menu, but i also have a do while loop after this, so whenever I enter 9 it just exits the current to while loop and goes on to the next one, is there anyway that I can skip past all the proceeding do while loops after I press 9, or even exit the method?

In your case continue or break could be useful. But if you want to exit the method you could just do it with return .

Try this, I have added break; when user enter "9" as an input. It will then come out of the do while loop.

            do {
                System.out.println("\nPlease enter your memberID (Press 9 to exit)");
                int bookID = input.nextInt();
                input.nextLine();
                if (bookID == 9) {
                    successful = true;
                    break;
                }

EDIT:

Yes to Check for the next loop, add a condition with that loop

loop(your original condition && !successful)
{
 }

EDIT 2: TO come out of the loop and avoid other do while loops in the method, you can just return; It will take back the control of the program through which this method was called.

do {
                    System.out.println("\nPlease enter your memberID (Press                       9 to exit)");
                    int bookID = input.nextInt();
                    input.nextLine();
                    if (bookID == 9) {
                        successful = true;
                       return;
                    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM