简体   繁体   English

避免多功能呼叫?

[英]Avoiding Multiple Function Calls?

I cannot figure out how to make it so it only calls check4 once... this was for a homework assignment last semester and I got 5 points off for calling it multiple times but I would not like to know how to do it (the professor never said how). 我无法弄清楚如何使它所以只要求check4一次......这是一个家庭作业的最后一个学期,我得到了5分关闭调用它多次,但我不会想知道如何做到这一点(教授从未说过怎么样。

I tried moving the check4 to after the if block but it really needs to go in between the last else if and the else which is not possible. 我尝试将check4移动到if块之后,但它确实需要介入最后一个if和else之间,这是不可能的。 The ONLY way the number should print is if all of the steps do not print out a word instead. 数字应该打印的唯一方式是,如果所有步骤都不打印出单词。

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

}

I hope I got your problem right so: you create a global boolean variable named say isMethodCalled witch is false, then in the check4 method you make it true, and simple check if the isMethodCalled is false before calling the method. 我希望我的问题是正确的:你创建了一个名为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;


}

First, I would update check4 to return a boolean (which you can save). 首先,我会更新check4return一个boolean (您可以保存)。 Something like, 就像是,

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

Then you can also save your tests into boolean variables. 然后,您还可以将测试保存到boolean变量中。 Something like 就像是

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

Simply use a flag, set it to true initially. 只需使用标志,最初将其设置为true

Then, wherever you dont want the check4 to run, set it to false . 然后,无论您何时不想运行check4,请将其设置为false

after the if-else, check if the flag is true . 在if-else之后,检查flag是否为true if it is, execute 'check4(printNumber)' 如果是,执行'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");
    }
}

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM