Home > OS >  while loop doesn't go to the next condition which is rows <=12, it's always true when r
while loop doesn't go to the next condition which is rows <=12, it's always true when r

Time:11-06

while statement doesn't go the second condition, it tests for the first condition but consider the second condition always true for some reason.

package com.company;
import java.util.*;

public class Main {

    public static void main(String[] args) {
        int rows;
        Scanner myObj = new Scanner(System.in);  // Create a Scanner object
        System.out.print("Enter rows between 5 and 12 inclusive :  ");
        rows = myObj.nextInt();

        while (rows < 5 && rows <=12){

            System.out.print("Enter rows between 5 and 12 inclusive :  ");
            rows = myObj.nextInt();
        }
        System.out.println("you can proceed now!");


    }
}      

CodePudding user response:

The condition should be lower than 5 OR higher than 12

while (rows < 5 || rows > 12) {
    System.out.print("Enter rows between 5 and 12 inclusive :  ");
    rows = myObj.nextInt();
}

CodePudding user response:

You said between 5 and 12 so it should be greater than 5 to less than 12

while (rows > 5 && rows > 12) {
    System.out.print("Enter rows between 5 and 12 inclusive :  ");
    rows = myObj.nextInt();
}

Now if you want to check both the correct conditions use || and if you want one of the conditions to be correct use &&

CodePudding user response:

(rows > 4 && rows < 13)

? > 5 inclusive

Any other hiccups use full name for java.util.WhateverObjectNameItHas not the import for java.util.*

  • Related