Java 避免多个函数调用?
我不知道怎么做,所以它只调用Java 避免多个函数调用?,java,function,Java,Function,我不知道怎么做,所以它只调用check4一次。。。这是上学期的一个家庭作业,我因为多次打电话而被打了5分,但我不想知道怎么做(教授从未说过怎么做) 我尝试在if块之后将check4移动到,但它确实需要介于最后一个else if和else之间,这是不可能的。打印数字的唯一方法是,如果所有步骤都没有打印出一个单词 public class CheeseCakeFactory_ajh187 { public static void main(String[] args) {
check4
一次。。。这是上学期的一个家庭作业,我因为多次打电话而被打了5分,但我不想知道怎么做(教授从未说过怎么做)
我尝试在if块之后将check4移动到,但它确实需要介于最后一个else 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");
}
}
}
public class CheeseCakeFactory_ajh187{
公共静态void main(字符串[]args){
int计数器=0;
int printNumber=0;//将更改为其中一个术语的数字
而(counter!=15&printNumber<210){//只要计数器不是15且printNumber小于210,它将保持循环。
printNumber++;
如果(打印编号%3==0&&printNumber%5==0&&printNumber%7==0){
系统输出打印(“cheesecakefactory”);
}
else if(printNumber%3==0&&printNumber%5==0){
系统输出打印(“芝士蛋糕”);
检查4(打印号码);
}
else if(printNumber%3==0&&printNumber%7==0){
系统输出打印(“奶酪工厂”);
检查4(打印号码);
}
else if(printNumber%5==0&&printNumber%7==0){
系统输出打印(“factorycake”);
检查4(打印号码);
}
else if(打印编号%3==0){
系统输出打印(“奶酪”);
检查4(打印号码);
}
else if(打印编号%5==0){
系统输出打印(“蛋糕”);
检查4(打印号码);
}
else if(打印编号%7==0){
系统输出打印(“工厂”);
检查4(打印号码);
}
else{//如果这个数不能被任何其他数整除,我们仍然需要检查4
if(Integer.toString(printNumber).contains(“4”)){
系统输出打印(“hoho”);
}
否则{
System.out.print(printNumber);//如果不能被4整除,我们只打印数字
}
}
系统输出打印(“”);
计数器++;
如果(counter==15){//一旦计数器为15,我们需要将新项目放在新行上
系统输出打印(“\n”);
counter=0;//重置计数器,以便我们可以每15次完成一次。
}
}
}
公共静态无效检查4(整数打印编号){
if(Integer.toString(printNumber).contains(“4”)){
系统输出打印(“hoho”);
}
}
}
我希望我正确地解决了您的问题,因此:
创建一个名为say isMethodCalled witch is false的全局布尔变量,然后在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;
}
首先,我将
check4
更新为return
aboolean
(可以保存)。大概
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.
}
}
}
boolean mod3=printNumber%3==0;
布尔mod5=printNumber%5==0;
布尔mod7=printNumber%7==0;
如果(mod3 | | mod5 | | mod7){
if(mod3&&mod5&&mod7){
系统输出打印(“cheesecakefactory”);
}否则{
布尔值isCheck4=check4(printNumber);//只需使用一个标志,最初将其设置为true
然后,在不希望运行check4的地方,将其设置为false
在if-else之后,检查标志是否为true
。如果是,则执行“check4(printNumber)”
public class CheeseCakeFactory_ajh187{
公共静态void main(字符串[]args){
int计数器=0;
int printNumber=0;//将更改为其中一个术语的数字
int flag=true;
而(counter!=15&printNumber<210){//只要计数器不是15且printNumber小于210,它将保持循环。
printNumber++;
如果(打印编号%3==0&&printNumber%5==0&&printNumber%7==0){
系统输出打印(“cheesecakefactory”);
flag=false;
}
else if(printNumber%3==0&&printNumber%5==0){
系统输出打印(“芝士蛋糕”);
}
else if(printNumber%3==0&&printNumber%7==0){
系统输出打印(“奶酪工厂”);
}
else if(printNumber%5==0&&printNumber%7==0){
系统输出打印(“factorycake”);
}
else if(打印编号%3==0){
系统输出打印(“奶酪”);
}
else if(打印编号%5==0){
系统输出打印(“蛋糕”);
}
else if(打印编号%7==0){
系统输出打印(“工厂”);
}
else{//如果这个数不能被任何其他数整除,我们仍然需要检查4
if(Integer.toString(printNumber).contains(“4”)){
系统输出打印(“hoho”);
}
否则{
System.out.print(printNumber);//如果不能被4整除,我们只打印数字
}
flag=false;
}
国际单项体育联合会(旗)
检查4(打印号码);
系统输出打印(“”);
计数器++;
如果(counter==15){//一旦计数器为15,我们需要将新项目放在新行上
系统输出打印(“\n”);
counter=0;//重置计数器,以便我们可以每15次完成一次。
}
}
}
公共静态无效检查4(整数打印编号){
if(Integer.toString(printNumber).contains(“4”)){
系统输出打印(“hoho”);
}
}
}
有很多方法可以实现,但您需要粘贴相关的
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");
}
}
}