0

I have programmed all night long and I can't find a mistake anymore :/ :/ If intro == 1 or 2 then do this and that.. and if intro != 1 or 2 then ask number again, till the number is 1 or 2... I can't find the mistake :/

   do{
        System.out.println("Enter number: ");
        Scanner scanintro = new Scanner(System.in);
        intro = scanintro.next();
    }while (intro == ("1")|| intro == ("2"));
KNO3
  • 23
  • 4

2 Answers2

5
do{
    System.out.println("Enter number: ");
    Scanner scanintro = new Scanner(System.in);
    intro = scanintro.nextInt();
} while (intro == 1|| intro == 2);
Yashar Panahi
  • 2,816
  • 1
  • 16
  • 22
1
do {
    System.out.println("Enter number: ");
    Scanner scanintro = new Scanner(System.in);
    intro = scanintro.next();
} while (!intro.equals("1") && !intro.equals("2"));

You should use equals to compare strings, but its an int(I don't know what the datatype of your intro variable is, but you should change it to an int) so you can check it like this:

do {
    System.out.println("Enter number: ");
    Scanner scanintro = new Scanner(System.in);
    intro = scanintro.next();
} while (intro != 1 || intro != 2);
Bas
  • 4,423
  • 8
  • 36
  • 53