避免多功能呼叫?

时间:2016-02-13 01:33:45

标签: java function

我无法弄清楚如何制作它所以它只调用check4一次......这是上个学期的作业,我多次调用5分但是我会没有喜欢知道怎么做(教授从未说过如何)。

我尝试将check4移动到if块之后,但它确实需要介入最后一个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)

我希望我的问题是正确的: 你创建了一个名为isMethodCalled的全局布尔变量,这是假的,然后在check4方法中你将其设为true,并在调用方法之前简单检查isMethodCalled是否为false。

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 :(得分:0)

首先,我会将check4更新为return一个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.
        }
    }
}

答案 2 :(得分: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");
    }
}

}