2016-02-13 67 views
3

我不知道如何使它,所以它只呼籲check4一次......這是爲上一學期的作業,我得到5分,多次呼叫它,但我喜歡知道如何做它(教授從未說過如何)。避免多個函數調用?

我試着將check4移動到if塊之後,但它確實需要在最後一個else之間進行,如果和其他不可能的話。數字應該打印的唯一方式是如果所有步驟都不打印出單詞。

public class CheeseCakeFactory_ajh187 { 
    public static void main(String[] args) { 

     int counter = 0; 
     int printNumber = 0; //number that will get changed to one of the terms 

     while (counter != 15 & printNumber < 210) { //as long as the counter is not 15 and print number is lesss than 210 it will keep looping. 
      printNumber++; 

      if (printNumber % 3 ==0 && printNumber % 5 == 0 && printNumber % 7 == 0) { 
       System.out.print("cheesecakefactory"); 
      } 
      else if (printNumber % 3 == 0 && printNumber % 5 == 0){ 
       System.out.print("cheesecake"); 
       check4(printNumber); 
      } 
      else if (printNumber % 3 == 0 && printNumber % 7 == 0){ 
       System.out.print("cheesefactory"); 
       check4(printNumber); 
      } 
      else if (printNumber % 5 == 0 && printNumber % 7 == 0){ 
       System.out.print("factorycake"); 
       check4(printNumber); 
      } 
      else if (printNumber % 3 == 0){ 
       System.out.print("cheese"); 
       check4(printNumber); 
      } 
      else if (printNumber % 5 ==0){ 
       System.out.print("cake"); 
       check4(printNumber); 
      } 
      else if (printNumber % 7 ==0){ 
       System.out.print("factory"); 
       check4(printNumber); 
      } 
      else { //if the number is not divisible by any of the other numbers we still have to check for the 4 
       if (Integer.toString(printNumber).contains("4")) { 
        System.out.print("hoho"); 
       } 
       else { 
        System.out.print(printNumber); //if its not divisible by 4, we just print the number 
       } 
      } 
      System.out.print(" "); 
      counter++; 
      if (counter == 15) { //once the counter is 15 we need to put the new items on a new line 
       System.out.print("\n"); 
       counter = 0; //resets the counter so that we can accomplish this every 15 passes. 
      } 
     } 
    } 

    public static void check4(int printNumber) { 
     if (Integer.toString(printNumber).contains("4")) { 
      System.out.print("hoho"); 
     } 
    } 

} 
+3

有很多方法可以完成,但需要在這裏粘貼相關代碼(而不是用於非現場鏈接)。 –

+0

固定..道歉 –

+0

這些都是其他條款?所以check4總是隻會被調用一次? –

回答

0

首先,我將更新check4return一個boolean(可以保存)。類似的,

public static boolean check4(int printNumber) { 
    return String.valueOf(printNumber).contains("4"); 
} 

然後,您還可以將您的測試保存到boolean變量中。像

boolean mod3 = printNumber % 3 == 0; 
boolean mod5 = printNumber % 5 == 0; 
boolean mod7 = printNumber % 7 == 0; 
if (mod3 || mod5 || mod7) { 
    if (mod3 && mod5 && mod7) { 
     System.out.print("cheesecakefactory"); 
    } else { 
     boolean isCheck4 = check4(printNumber); // <-- call it once 
     if (mod3 && mod5) { 
      System.out.print("cheesecake"); 
     } else if (mod3 && mod7) { 
      System.out.print("cheesefactory"); 
     } else if (mod5 && mod7) { 
      System.out.print("factorycake"); 
     } else if (mod3) { 
      System.out.print("cheese"); 
     } else if (mod5) { 
      System.out.print("cake"); 
     } else if (mod7) { 
      System.out.print("factory"); 
     } else { 
      if (!isCheck4) { // <-- it doesn't have a 4, print it. 
       System.out.print(printNumber); 
      } 
     } 
     if (isCheck4) { 
      System.out.print("hoho"); // <-- it does have a 4. 
     } 
    } 
} 
+0

這是美麗的。非常感謝你 –

+0

這是一個怎樣的改進?我更喜歡原來的版本。 – emory

0

我希望我得到了你的問題所在,以便: 創建一個名爲說isMethodCalled女巫是假的,那麼在check4方法,你讓它真正的全球性布爾變量,以及簡單的檢查,如果isMethodCalled是之前調用假方法。

boolean isMethodCalled = false; 

if(!isMethodCalled) { 
    check4() // do wathever you need to do to call check4() 
} 



public static void check4(int printNumber) { 
    if (Integer.toString(printNumber).contains("4")){ 

     System.out.print("hoho"); 
    } 

    isMethodCalled = true; 


} 
+1

好教授認爲我太多參考了這個功能。她表示,我不必在每個其他人都check4,如果..和多數民衆贊成的問題。但是,它的工作原理與目前所寫的相同。 –

0

東西只需使用一個標誌,將其設置爲true開始。

然後,無論您不希望check4運行,將其設置爲false

if-else之後,檢查flag是否爲true。如果是,請執行'check4(printNumber)'

public class CheeseCakeFactory_ajh187 { 
public static void main(String[] args) { 

    int counter = 0; 
    int printNumber = 0; //number that will get changed to one of the terms 
    int flag=true; 

    while (counter != 15 & printNumber < 210) { //as long as the counter is not 15 and print number is lesss than 210 it will keep looping. 
     printNumber++; 

     if (printNumber % 3 ==0 && printNumber % 5 == 0 && printNumber % 7 == 0) { 
      System.out.print("cheesecakefactory"); 
    flag=false; 
     } 
     else if (printNumber % 3 == 0 && printNumber % 5 == 0){ 
      System.out.print("cheesecake"); 
     } 
     else if (printNumber % 3 == 0 && printNumber % 7 == 0){ 
      System.out.print("cheesefactory"); 
     } 
     else if (printNumber % 5 == 0 && printNumber % 7 == 0){ 
      System.out.print("factorycake"); 
     } 
     else if (printNumber % 3 == 0){ 
      System.out.print("cheese"); 
     } 
     else if (printNumber % 5 ==0){ 
      System.out.print("cake"); 
     } 
     else if (printNumber % 7 ==0){ 
      System.out.print("factory"); 
     } 
     else { //if the number is not divisible by any of the other numbers we still have to check for the 4 
      if (Integer.toString(printNumber).contains("4")) { 
       System.out.print("hoho"); 
      } 
      else { 
       System.out.print(printNumber); //if its not divisible by 4, we just print the number 
      } 
    flag=false; 
     } 

    if(flag) 
    check4(printNumber); 
     System.out.print(" "); 
     counter++; 
     if (counter == 15) { //once the counter is 15 we need to put the new items on a new line 
      System.out.print("\n"); 
      counter = 0; //resets the counter so that we can accomplish this every 15 passes. 
     } 
    } 
} 

public static void check4(int printNumber) { 
    if (Integer.toString(printNumber).contains("4")) { 
     System.out.print("hoho"); 
    } 
} 

}