【问题标题】:"If" Statement Will Not Print in Loop“If”语句不会循环打印
【发布时间】:2020-01-08 07:16:44
【问题描述】:

我的代码有效,只有一个问题。该代码旨在打印两个用户输入之间的数字。这部分代码有效,但是如果第一个数字大于第二个数字,则意味着不会打印并再次询问。到目前为止一切正常,但是如果第一个数字大于第二个数字,控制台和代码就会结束,我不知道为什么?你们能帮忙解释一下我做错了什么吗?谢谢!这是我的代码:

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++);
}

【问题讨论】:

  • 提示:如果lowerNum 大于higherNum,则while 循环的条件将从一开始就计算为false
  • 当你的循环检查相反时,应该如何检查 if ?如果你想至少执行一次 if 来检查输入,你想使用 do-while 循环
  • while 和 if 语句的条件是互补的:如果 while 条件为真,if 条件为假。

标签: java loops if-statement printing while-loop


【解决方案1】:

只有在higherNum >= lowerNum 时才会开始循环,这意味着循环内的if 条件永远不会为真。

要实现您的输出,您应该执行以下操作。

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++);
        }

【讨论】:

    【解决方案2】:

    我建议你先使用你的 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++);
        }
    }
    

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

    【讨论】:

      【解决方案3】:

      你的 if 应该在 while 之前。我在路上修好了一些其他的东西。

      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);
                  }
      
              }
          }
      }
      

      【讨论】:

        【解决方案4】:

        您可以使用重新启动过程的方法来输入数字。递归选项将有助于该问题。

        公共无效 TakeNumbers(){ 扫描仪阅读器 = 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();
        

        }

        【讨论】:

        • 哦,好吧,我还没有学会那个命令。感谢您的意见!
        【解决方案5】:

        它会在 for 循环中打印以进行确认,只需通过以下链接

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

        【讨论】: