-1

I want to redirect the flow of code to "1" part after the else statement.

I have tried to achieve it by creating loops and different methods but I couldn't make it.

The thing that I want is, it will give an error when an invalid answer is given and will ask the questions again.

import java.util.Scanner;

public class OddsAndEvens {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("Let’s play a game called “Odds and Evens”");
        System.out.println("What is your name?");
        String name = input.next();

        // 1 <------------------------

        System.out.println("Hello " + name + " which one do you choose? (O)dds or (E)vens?"); 

        String oe = input.next();

        if (oe.equalsIgnoreCase("e")) {
            System.out.println(name + " has picked evens! The computer will be odds.");
        }
        if (oe.equalsIgnoreCase("o")) {
            System.out.println(name + " has picked odds! The computer will be evens.");
        }

        else {
            System.out.println("You have typed an invalid answer.");

        }


    }

}
General Grievance
  • 4,555
  • 31
  • 31
  • 45
E.Canberk
  • 9
  • 4

2 Answers2

0

check the updated code and try to read about while loops

import java.util.Scanner;

public class OddsAndEvens {

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.println("Let’s play a game called “Odds and Evens”");
    System.out.println("What is your name?");
    String name = input.next();
    do {
        System.out.println("Hello " + name + " which one do you choose? (O)dds or (E)vens?");

        String oe = input.next();

        if (oe.equalsIgnoreCase("e")) {
            System.out.println(name + " has picked evens! The computer will be odds.");
            break; // after any valid input we will break it
        } else if (oe.equalsIgnoreCase("o")) {
            System.out.println(name + " has picked odds! The computer will be evens.");
            break;// after any valid input we will break it
        } else {
            System.out.println("You have typed an invalid answer.");
        }
    } while (true); // this OO loop

}

}
Mohamed Fouad
  • 111
  • 1
  • 2
  • 10
0

The fix is simple, see below

    import java.util.Scanner;

    public class OddsAndEvens {
        public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
            System.out.println("Let’s play a game called “Odds and Evens”");
            System.out.println("What is your name?");
            String name = input.next();
            while(true) {
            // 1 <------------------------

            System.out.println("Hello " + name + " which one do you choose? (O)dds or (E)vens?"); 

            String oe = input.next();

            if (oe.equalsIgnoreCase("e")) {
                System.out.println(name + " has picked evens! The computer will be odds.");
                //do even logic here
                break;
            }
            if (oe.equalsIgnoreCase("o")) {
                System.out.println(name + " has picked odds! The computer will be evens.");
                //do odd logic here
                break;
            }

            else {
                System.out.println("You have typed an invalid answer, lets try again");

            }
        }


        }

    }
Ankur Agarwal
  • 23,692
  • 41
  • 137
  • 208