简体   繁体   English

“If”语句不会循环打印

[英]"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.您的循环仅在 highNum >=lowerNum 时开始,这意味着循环内的 if 条件永远不会为真。

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 条件:

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.发生这种情况是因为如果lowerNum大于higherNum您的while 条件将导致false 并且不会打印任何内容,因为错误消息在while 循环内。

Your if should be before while.你的 if 应该在 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); 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) {公共静态无效主(字符串 [] args){

TakeNumbers();

} }

ya it will print inside for loop for confirmation just go through the following link是的,它会在 for 循环内打印以进行确认,只需通过以下链接

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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