代码中循环的输出不正确

时间:2015-04-18 01:14:17

标签: java methods output

我一直在为我为作业创建的这种方法而苦苦挣扎,这让我疯狂。我的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;
    }
}

8 个答案:

答案 0 :(得分:0)

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

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

  4. 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;
        }
    }
    

答案 1 :(得分:0)

循环中的所有内容都会执行循环运行的次数。在这种情况下,循环执行3次,因为这是你设置的(int i = 1; i&lt; 4; i ++)。 线条&#34; Lather and Rise&#34;和&#34;完成&#34;多次打印,因为它们也在该循环内。 此外,您没有设置Sentinal以防止最后两个&#34; 2:泡沫和冲洗。&#34;从打印。如果您不打印任何内容,请设置一个案例。

答案 2 :(得分:0)

线路:每次循环运行时都会输出'Lather and Rise',因为它不在if / else块内。与'完成'相同的事情。将此行放在for循环之外。另外要指出的是,而不是硬编码4,将i&lt; numCycles。因此,在您要显示2个“Lather and rinse”循环的情况下,numCycles应为2.

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

答案 3 :(得分:0)

    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 for循环,它将打印 Lather and Rinse 声明随后完成并且有效。

答案 4 :(得分:0)

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

编写一个方法printShampooInstructions(),其参数为int numCycles,返回类型为void。如果numCycles小于1,则打印“太少”。如果大于4,则打印“太多”。否则,打印“ N:起泡并冲洗”。 numCycles次,其中N是循环数,后跟“完成”。以换行符结尾。输入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);
   }
}

答案 5 :(得分:0)

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

答案 6 :(得分:0)

公共类洗发方法{

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

}

答案 7 :(得分:0)

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