-3

I'm looking for a way to ask people if their sure about their gender, if not, I'd like to be able to re-ask the question.

    System.out.println("Enter your gender: ");
    String gender = "";
    while (true) {
        gender = Gender.nextLine();
        if (gender.equals("1")) {
            System.out.println("A lad, are you sure?");
            break;
        } else if (gender.equals("2")) {
            System.out.println("A gal, are you sure?");
            break;
        } else System.out.println("Please enter a valid entry: ");
  • 2
    One option is `continue`. – Andy Thomas Mar 15 '19 at 17:00
  • Show us what you have written so far, please. – deHaar Mar 15 '19 at 17:00
  • 1
    The answer is in your question. '*how do I make it **`continue`** or go back*' – Nicholas K Mar 15 '19 at 17:01
  • 1
    Try to share code here, edit your question again, else it will considered here as you tried here to get homework done from other users.. –  Mar 15 '19 at 17:03
  • Not really, @NicholasK, if I understand. The `contimue` statement goes back to the beginning of the loop, where I think the asker by *continue* means continue *after the end* of the loop (what the `break` statement does).. – Ole V.V. Mar 15 '19 at 17:09

2 Answers2

1

I don’t think you need to do anything special. The point in a loop is that it goes back to the beginning when it reaches the end. Something like:

    boolean sure;
    do {

        // ...

        sure = // ...

    } while (! sure);

Your question is answered more directly in the comments already: the continue; statement goes back to the beginning of the enclosing loop:

    do {

        // ...

        sure = // ...

        if (! sure) {
            continue;
        }            

        // ...

    } while (someCondition);
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
1

You could use continue statement as it is used to jump immediately to the next iteration in a loop. Just put continue; after the statement from where you want to return back to the beginning of the loop.