【问题标题】:Incorrect Output from Loop in Code代码中的循环输出不正确
【发布时间】:2015-04-18 01:14:17
【问题描述】:

我一直在努力使用我为作业创建的这种方法,它让我发疯。我的 if/else 语句有问题,但我似乎无法找出它是什么。这是我收到的输出:

1: Lather and Rise.
2: Lather and rinse.
Done
2: Lather and Rise.
2: Lather and rinse.
Done
3: Lather and Rise.
2: Lather and rinse.
Done

我希望看到这个:

1: Lather and rinse.
2: Lather and rinse.
Done.

我做错了什么来阻止它正确输出?任何建议/帮助表示赞赏!请参阅下面的代码:

public class ShampooMethod {

    public static void printShampooInstructions(int numCycles) {

        for (int i = 1; i < 4; i++) {
            System.out.println(i + ": Lather and Rise.");
            if (numCycles < 1) {
                System.out.println("Too few.");
            } else if (numCycles >= 4) {
                System.out.println("Too many");
            } else {
                System.out.println(numCycles + ": Lather and rinse.");
            }
            System.out.println("Done");
        }
    }


    public static void main(String[] args) {
        printShampooInstructions(2);

        return;
    }
}

【问题讨论】:

  • 请缩进您的代码以便阅读。
  • 会不会是循环 3 次的 for 循环包裹了你的所有代码?
  • 此行最先打印System.out.println(i + ": Lather and Rise.");,所以Rise 总是第一个被打印
  • 循环 3 次怎么能显示一次呢?
  • 在 for 循环中放 i

标签: java methods output


【解决方案1】:

6.4.2:带循环的方法:洗发水。

编写一个方法 printShampooInstructions(),带有 int 参数 numCycles,返回类型为 void。如果 numCycles 小于 1,则打印“太少。”。如果超过 4 个,打印“Too many.”。否则,打印“N:起泡并冲洗”。 numCycles 次,其中 N 是循环数,后跟“Done.”。以换行符结束。输入 2 的示例输出:

1:起泡并冲洗。

2:起泡并冲洗。

完成。

import java.util.Scanner;

public class ShampooMethod {

   /* Your solution goes here  */
   public static void printShampooInstructions(int numCycles) {
      if (numCycles < 1){
      System.out.println("Too few.");   
      }
      else if (numCycles > 4){
      System.out.println("Too many.");
      } 
      else {
         for(int i = 1; i <= numCycles; i++){
            System.out.println(i + ": Lather and rinse.");
         }
         System.out.println("Done.");
      }
   }

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

      userCycles = scnr.nextInt();
      printShampooInstructions(userCycles);
   }
}

【讨论】:

    【解决方案2】:

    循环中的所有内容都会按照循环运行的次数执行。在这种情况下,循环执行 3 次,因为这是您设置的 (int i = 1; i

    【讨论】:

      【解决方案3】:

      每次循环运行时都会输出“Lather and Rise”行,因为它不在 if/else 块内。与“完成”相同。将此行放在 for 循环之外。需要指出的另一件事,不是硬编码 4,而是将 i

      public static void printShampooInstructions(int numCycles) {
      
          for (int i = 0; i < numCycles; i++) {
              // System.out.println(i + ": Lather and Rise.");
              if (numCycles < 1) {
                  System.out.println("Too few.");
              } else if (numCycles >= 4) {
                  System.out.println("Too many");
              } else {
                  System.out.println(numCycles + ": Lather and rinse.");
              }   
          }
          System.out.println("Done");
      }
      

      【讨论】:

        【解决方案4】:

        查看下面的 cmets,看看是什么导致了与您预期不同的输出。

        1. 您一次迭代太多了
        2. System.out.println(i + ": Lather and Rise."); 将显示在循环的每一轮中,因为您没有使用ifelseelse if 过滤
        3. 在循环之后切换System.out.println("Done"); 的位置 - 因为如果循环完成并且您处于方法的末尾,那么您就完成了。

        public class ShampooMethod {
        
            public static void printShampooInstructions(int numCycles) {
        
                // i = 1 - i < (2+1=3) -> i will become 1 and than 2 -> done
                for (int i = 1; i < (numCycles+1); i++) {
                    // this would be displayed every time you go throw the loop
                    // System.out.println(i + ": Lather and Rise.");
                    if (numCycles < 1) {
                        System.out.println("Too few.");
                    } else if (numCycles >= 4) {
                        System.out.println("Too many");
                    } else {
                        System.out.println(i + ": Lather and rinse.");
                    }
                }
                // Done goes here because only after the loop we are done
                System.out.println("Done");
            }
        
        
            public static void main(String[] args) {
                printShampooInstructions(2);
        
                return;
            }
        }
        

        【讨论】:

          【解决方案5】:
              if (numCycles < 1) {
                 System.out.println("Too few.");
                }
              else if (numCycles > 4) {
                 System.out.println("Too many.");
                }
              else {
                 for (i = 1; i <= numCycles; ++i) {
                    System.out.println(i + ": Lather and rinse.");
                   }
                  System.out.println("Done.");
                }
          

          我使用了一个 if else 语句,并且在 else 实例的语句末尾,我创建了一个 for 循环 来打印Lather and Rinse 语句,然后是 done,它起作用了。

          【讨论】:

            【解决方案6】:
                import java.util.Scanner;
                public class Methods_ShampooMethod_ChallengeActivity {
            
                    public static void printShampooInstructions(int numCycles) {
                        
            
                        if (numCycles < 1) {
                            System.out.println("Too few.");
                        }
                        else if (numCycles > 4) {
                            System.out.println("Too many.");
                        }
                        else {
                            for (int i = 1; i <= numCycles; i++) {
                                System.out.println(i + ": Lather and rinse.");         
                            }
                        System.out.println("Done.");           
                        }                
                    }
            
                    public static void main (String [] args) {
                        Scanner scnr = new Scanner(System.in);
                        int userCycles;
                    
                        System.out.print("Enter cycles: ");
                        userCycles = scnr.nextInt();
                    
                        printShampooInstructions(userCycles);   
                    }
                }
            

            【讨论】:

              【解决方案7】:

              公共类 ShampooMethod {

              public static void printShampooInstructions(int numCycles) {
                  if (numCycles < 1) {
                      System.out.println("Too few.");
                  } else if (numCycles > 4) {
                      System.out.println("Too many.");
                  } else {
                      for (int i = 1; i <= numCycles; i++) {
                          System.out.println(i + ": Lather and rinse.");
                      }
                      System.out.println("Done.");
                  }
              }
              
              public static void main(String[] args) {
                  Scanner scnr = new Scanner(System.in);
                  int userCycles;
              
                  userCycles = scnr.nextInt();
                  printShampooInstructions(userCycles);
              }
              

              }

              【讨论】:

                【解决方案8】:

                在第一个 for 循环中输入: for (int i = 1; i &lt; numCycles; i++) 而不是这个: for (int i = 1; i &lt; 4; i++)

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 2014-04-08
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2014-04-12
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多