2015-04-18 33 views
0

我一直在爲這項任務創建的這種方法掙扎,而且它讓我瘋狂。我的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; 
    } 
} 
+0

請縮進您的代碼以使其可以閱讀。 –

+0

難道它是循環迭代3次,它是圍繞你的所有代碼? – kirbyquerby

+3

這行首先打印出'System.out.println(i +「:Lather and Rise。」);'這樣Rise總是第一個被打印的 –

回答

0

看看下面的評論,看看是什麼導致了不同的輸出比預期。因爲你用ifelseelse if

  • 交換爲System.out.println("Done");後位置不過濾

    1. 你是一個迭代時間多
    2. System.out.println(i + ": Lather and Rise.");將顯示在每一輪循環循環 - 因爲如果循環完成並且您處於方法的末尾,您就完成了。

    在一個循環
    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; 
        } 
    } 
    
  • 0

    一切都被執行的循環的運行次數的數量。在這種情況下,循環執行3次,因爲這是你所設置的(int i = 1; i < 4; i ++)。 「Lather and Rise」和「Done」行被打印多次,因爲它們也在該循環中。 你也沒有設置防止最後兩個「2:泡沫和沖洗」。從打印。如果你不打印任何東西,請設置一個案例。

    0

    在每次運行循環時都會輸出'Lather and Rise',因爲它不在if/else塊中。 '完成'也是一樣。把這條線放在for循環之外。另一點需要指出的是,不是硬編碼4,而是編號爲<的numCycles。所以,在你想顯示的2個週期「泡沫和沖洗」,你的情況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"); 
    } 
    
    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."); 
         } 
    

    我用了一個如果其他聲明,對在實例聲明的尾巴else,我創建了一個for循環這將打印泡沫和沖洗聲明,然後完成和它的工作。