(BEGINNER) JAVA do...while loop logic broken?

tilenbox :

In the code bellow I make a do..while loop that is supposed to repeat if the value is between 1000 and 9999, but the logic of the do..while loop seems off.

import java.util.Scanner;
public class Nal2 {
public static void main(String[] ARGS) {
    int i = 0;
    Scanner bralnik = new Scanner(System.in);
    do {
        System.out.printf("Insert a 4 digit num: ");
        i = bralnik.nextInt();
    } while (i >= 1000 && i <= 9999);

    System.out.print("Oi");
  }
}

This dosen't work but the code bellow works.

import java.util.Scanner;
public class Nal2 {
public static void main(String[] ARGS) {
    int i = 0;
    Scanner bralnik = new Scanner(System.in);

    do {
        System.out.printf("Insert a 4 digit num: ");
        i = bralnik.nextInt();

    } while (i <= 1000 || i >= 9999);

    System.out.print("Oi");
 }
}

Shouldn the && works instead of the ||?

I'm really confused about this.

I'm using InteliJ IDEA 2019.3.4 CE

Ulvi Ibrahimli :
   public static void main(String[] args) {
        int i = 0;
        Scanner bralnik = new Scanner(System.in);

        do {
            System.out.printf("Insert a 4 digit num: ");
            i = bralnik.nextInt();


        } while (!(i<=1000||i>=9999));

        System.out.print("Oi");
    }

if you write code just with OR(||), you write above. ! is Logical not Reverse the result, returns false if the result is true. Your second snippet does not work because, you write OR, OR returns true if one of the conditions is true.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=370804&siteId=1