简体   繁体   中英

Getting the Code to Restart After Invalid Entry (Java)

I'm working on this block of code:

    while (!userCorrect) {
        Scanner input = new Scanner(System.in);
        System.out.print("Guessing (round " + roundNumber1++ + "): Choosing your letter from a-z: ");
            String letter = input.nextLine();
            if (letter.length () > 1) {
                System.out.println("You should not enter more than 1 character");
                } 


        System.out.println("end of (round " + roundNumber2++ + ")");
    }
    }

What I'm trying to do is after the user inputs more than 1 character, the code goes back to:

System.out.print("Guessing (round " + roundNumber1++ + "): Choosing your letter from az: ");

Until only 1 character is selected and then moves onto round 2.

Ex. assuming it's round 1 and the user inputs az. It'll throw my error saying that you should not enter more than 1 character and then it'll go back to:

Guessing(round 1): Choosing your character from az:

Any ideas? I got it to work but it's skipping to the next round and I want it to stay on the same round until a valid input. I've been searching for hours and can't get it to work.

Increment only after receiving valid input:

boolean userCorrect = false;
int roundNumber = 1;  // starting round

while (!userCorrect) {

    Scanner input = new Scanner(System.in);
    System.out.print("Guessing (round " + roundNumber + "): Choosing your letter from a-z: ");
    String letter = input.nextLine();

    if (letter.length () > 1) {
        System.out.println("You should not enter more than 1 character");
    }

    else{
        System.out.println("end of (round " + roundNumber + ")");
        roundNumber++;       // now increment
    }
}

Note this loop will continue until you set userCorrect = true somewhere within the loop.

you can use like below

while (!userCorrect) {
        Scanner input = new Scanner(System.in);
        System.out.print("Guessing (round " + roundNumber1 + "): Choosing your letter from a-z: ");
            String letter = input.nextLine();
            if (letter.length () > 1) {
                System.out.println("You should not enter more than 1 character");
                continue;
                } 

        System.out.println("end of (round " + roundNumber1 + ")");
        roundNumber1++;
    }

Here's another way, using the continue keyword which basically forces execution back up to the top of the loop. Using this implementation, simply set done to true when you want it to be done.

    public static void main(String[] args)
        {
            Scanner input = new Scanner(System.in);
            int roundNumber = 1;
            boolean done = false;

            do
            {
                System.out.println("Guessing (round " + roundNumber + "): Choosing your letter from a-z: ");
                String letter = input.nextLine();
                if(letter.length() > 1)
                {
                    System.out.println("You should not enter more than 1 character");
                    continue;
                }
                else
                {
                    roundNumber++;
                }

                //Set done to true based on some condition

            }while(!done);

        }
        //end main

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