简体   繁体   中英

"If" Statement Will Not Print in Loop

My code works, there's is just one problem. The code is meant to be about printing the numbers in between two user inputs. That part of the code works, however if the first number is greater than the second number it is meant to not print and ask again. Everything up to that point works, however if the first number is greater than the second, the console and code just end, and I cannot figure out why? Can you guys help and explain what I am doing wrong? Thanks! Here is my code:

public static void main(String[] args) {
    Scanner reader = new Scanner(System.in);

    int higherNum, lowerNum;

    System.out.print("First: ");
    lowerNum=Integer.parseInt(reader.nextLine());

    System.out.print("Second: ");
    higherNum=Integer.parseInt(reader.nextLine());

    while (higherNum>=lowerNum){
        if (lowerNum>higherNum){
            System.out.print("Sorry, you put your first number higher than your second, please make your first number a smaller number than your second. "); // this does not print 
        }
    }

    System.out.println(lowerNum++);
}

Your loop only starts if higherNum >= lowerNum, which means your if condition inside the loop will never be true.

To achieve your output, you should do something as following.

while (lowerNum>higherNum ){
            System.out.print("Sorry, you put your first number higher than your second,   please make your first number a smaller number than your second. "); // this does not print

            System.out.print("First: ");
            lowerNum=Integer.parseInt(reader.nextLine());

            System.out.print("Second: ");
            higherNum=Integer.parseInt(reader.nextLine());

        }

        while(lowerNum <= higherNum) {
            System.out.println(lowerNum++);
        }

I would suggest that you use your if condition first:

if (lowerNum>higherNum){
    System.out.print("Sorry, you put your first number higher than your second, please make your first number a smaller number than your second. ");
} else{
    while (higherNum>=lowerNum){
        System.out.println(lowerNum++);
    }
}

It's happening because if the lowerNum is bigger than the higherNum your while condition would result in false and won't print anything since the error message is inside the while loop.

Your if should be before while. I fixed some other things on the way.

public static void main(String[] args) {

    Scanner reader = new Scanner(System.in);
    int higherNum = 0, lowerNum = 1;

    while(lowerNum>=higherNum){

        System.out.print("First: ");
        lowerNum=Integer.parseInt(reader.nextLine());

        System.out.print("Second: ");
        higherNum=Integer.parseInt(reader.nextLine());

        if(lowerNum>=higherNum){

            System.out.println("Sorry, you put your first number higher than your second, please make your first number a smaller number than your second. ");

        } else {

            while(lowerNum<higherNum-1){
                System.out.println(++lowerNum);
            }

        }
    }
}

You can give the number inputs with a method for restarting the process. Recursive option would be helpful for that issue.

public void TakeNumbers(){ Scanner reader = new Scanner(System.in);

int higherNum, lowerNum;

System.out.print("First: ");
lowerNum=Integer.parseInt(reader.nextLine());

System.out.print("Second: ");
higherNum=Integer.parseInt(reader.nextLine());

 if (lowerNum>higherNum){
        System.out.print("Sorry, you put your first number higher than your second, please make your first number a smaller number than your second. ");

TakeNumbers();

System.out.println(lowerNum++);

}

public static void main(String[] args) {

TakeNumbers();

}

ya it will print inside for loop for confirmation just go through the following link

https://www.sanfoundry.com/java-program-check-given-number-perfect-number/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM