【问题标题】:Not understanding the code logic不懂代码逻辑
【发布时间】:2013-11-15 09:38:51
【问题描述】:

我想打印输出: 1 3 5 7 9 11 13 15 17 19 21 1 3 5 7 9 11 但我得到这个输出: 1 34 67 910 1213 1516 1819 211 34 67 910 1213 1516 1819 21

谁能解释一下我在逻辑上犯的错误?

public class BadNews {

    public static final int MAX_ODD = 21;

    public static void writeOdds() {    
    // print each odd number

        for ( int count = 1; count <= (MAX_ODD - 2); count++) {
            System.out.print(count + " ");

            count = count + 2;
            
            // print the last odd number
                    System.out.print(count);
        }
    }

    public static void main(String[] args) {
        // write all odds up to 21
        writeOdds();

        // now, write all odds up to 11
        writeOdds();

    }
}

【问题讨论】:

    标签: java for-loop logic


    【解决方案1】:

    首先,不需要第二个System.out.print。

    第二,我看到你上升到 21,但是你在代码中的什么地方指定你的第二次调用应该上升到 11?

    您可以通过将上限设为writeOdds 函数的参数来做到这一点:

    public static void writeOdds(int upperLimit)
    {
       for(int count = 1; count <= upperLimit; count += 2)
       ...
    

    然后您可以调用它两次,分别为 writeOdds(21) 和 writeOdds(11)。

    哦,而且,你可以取出count = count + 2,这是在for 循环中处理的。

    【讨论】:

    • 谢谢...我已经做出了必要的更改,但我的输出链中没有得到 21。它就像 1 3 5 7 9...19 1 3 5 7 9... 我在定义上限时犯了一些错误吗?
    • 对,-2 必须去! (修改了我的答案)
    【解决方案2】:

    问题是你正在做count++ 和count=count+2 所以你会打印一次奇数和一次偶数

    更新

    解决问题最快的方法是改成while循环

    int count = 1;
    while( count <= (MAX_ODD - 2)) {
    //rest of your code in loop
    }
    

    而且你只需要一张印刷品

    【讨论】:

      【解决方案3】:

      我在您的代码中看到了许多问题:

      • 两次更新循环变量:

      而是在for语句中更新一次为;

      for ( int count = 1; count <= (MAX_ODD - 2); count+=2)
      

      并删除该行

      count = count + 2;
      
      • 为什么要检查 MAX_ODD-2 ?

      检查count &lt;= MAX_ODD

      • 您希望在循环中有一个打印语句。删除线;

        //打印最后一个奇数

        System.out.print(count);

      • 按照@Roy Dictus 的建议,更改 writeOdds 方法以接受 max_odd 参数。

      【讨论】:

        【解决方案4】:

        第一个问题是您要更新计数器两次。
        其次,随叫随到,您将如何精确 MAX_ODD,因为它是一个静态变量。试试这个;

        public class BadNews {
        
        public static void writeOdds(int MAX_ODD) {    
        // print each odd number
        
            for ( int count = 1; count <= MAX_ODD; count+=2) {
        
                System.out.print(count + " ");
        
                // print the last odd number
                System.out.print(count);
            }
        }
        
        public static void main(String[] args) {
            // write all odds up to 21
            writeOdds(21); <-- Pass the MAX_ODD by parameter
        
            // now, write all odds up to 11
            writeOdds(11);
        
        }
        }
        

        【讨论】:

          【解决方案5】:

          他们不希望您使用参数方法,而是希望您使用 final int 加上单独的循环将赔率打印到 11

          public class BadNews {
              public static final int MAX_ODD = 21;
          
              public static void writeOdds() {
                  // print each odd number
                  for (int count = 1; count <= (MAX_ODD); count+=2) {
                      System.out.print(count + " ");
                  }
          
                  // print the last odd number
              }
          
              public static void main(String[] args) {
                  // write all odds up to 21
                  writeOdds();
                  System.out.println();
                  // now, write all odds up to 11
                  for (int count = 1; count <= 11; count+=2) {
                      System.out.print(count + " ");
                  }
              }
          }
          

          【讨论】:

            【解决方案6】:
            public class BadNews {
                public static final int MAX_ODD = 21;
                    public static void main(String[] args) {
                   
                    for (int count = 1; count <= MAX_ODD; count+=2) {
                        System.out.print(count + " ");
                            }
                         System.out.println();
            
                    for (int count = 1; count <=(MAX_ODD-10); count+=2) {
                        System.out.print(count + " ");
                         }
            }
            }
            

            【讨论】:

            • 如果你在实践中解决这个问题-它,那么它肯定有效
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2017-05-16
            • 2011-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-08-22
            • 2017-10-13
            • 2023-03-28
            相关资源
            最近更新 更多