Java 计算while循环中的条件语句运行的次数
这将计算当为true时while循环运行的次数,但是,我试图计算num==0测试执行的次数。num整数将更改Java 计算while循环中的条件语句运行的次数,java,Java,这将计算当为true时while循环运行的次数,但是,我试图计算num==0测试执行的次数。num整数将更改 int counter = 0; while (num == 0) { doSomething(); counter ++; } 这是我想出的另一个选择,但我正在寻找更好的方法 int counter = 0; do { if (num == 0) { doSomething(); counter++; } else {
int counter = 0;
while (num == 0) {
doSomething();
counter ++;
}
这是我想出的另一个选择,但我正在寻找更好的方法
int counter = 0;
do {
if (num == 0) {
doSomething();
counter++;
} else {
counter++;
}
} while (num == 0);
给定循环:
while (num == 0) {
doSomething();
counter ++;
}
检查num==0
将在每次迭代中执行一次,然后在最后一次迭代后执行一次。最后一个检查是防止再次执行时的
因此,如果计数器
等于3,这意味着我们有3次完整的循环迭代(3次检查num==0
),然后第四次检查阻止我们再次进入循环
我不清楚您的第二个代码片段想要实现什么
if (num == 0) {
doSomething();
counter++;
} else {
counter++;
}
100%等于
if (num == 0) {
doSomething();
}
counter++;
每次迭代do…while()
后,由于循环条件,仍将检查num==0
。如果要计算在while循环中检查(检查)特定条件的次数,请放置一个计数器变量(增加1:计数器++;
)直接位于循环中的if语句行上方,因为如果特定条件为真,任何条件语句都可能突然中断其代码块中的循环,或继续其代码块中的循环
int num = 0;
int counter = 0;
while(num==0) {
if (num == 0) {
doSomething();
} else {
}
counter++;
//if the counter inc then the above condition statement should been executed.Make sure this doesn't take break into consideration.
}
此计数器变量需要在while循环上方声明并初始化为0。以下是一个示例:
int counter = 0;
while (num == 0) {
if (doSomething == true) {
continue; // counter can not increment. Goes to next iteration.
}
if (doOtherThing.equals("no") {
break; // counter can not increment. Exits loop.
}
counter ++; // counts the number of times the following IF was checked.
if (n == 0) {
doSomthingCool();
num++; // increments num to 1 and loop will exit on next iteration attempt.
}
}
上面的计数器只是表示在该计数器的增量之后的IF条件肯定会被检查…不发现为真,并且其代码块被输入并运行。它基本上表示肯定会检查该条件
这很好,直到您开始处理else if。如果您想知道是否检查了else if?是检查了if还是else if?您仍然不想知道该条件是否为真,并且输入了该语句的代码块,您只想知道是否查看并检查了该条件
克服这种情况的一种方法是将计数器直接应用到该语句的条件中。这是可以做到的,但计数器条件部分应始终等于true,并且是遇到的第一个条件,后跟&&(和)在整个条件语句中,通过这种方式,所有其他条件将按其原始顺序进行检查:
if ((ifCounter == ifCounter++) && someNum < 3) { ... }
else if ((elseCounter = elseCounter++) && someNum < 10) { ... }
我不确定您试图实现什么,但是如果(num==0)
条件在第二、第三和以后的迭代中是多余的,因为这些条件只有在条件为而(num==0)时才会执行
将为真。是的,这是正确的,但我正在尝试计数它,不管num==0是否为真,while循环只是在那里调用doSomething。由于num
的值在这里从未更改,因此检查num==0
将被无限多次调用。并且由于无穷加上或减去任何数字都等于无穷,任何偏移错误在这里都无关紧要,因为计数器也将是无限的,所以您可以说计数器
实际上等于执行检查的次数。这意味着您已经是对的,但如果您想通过运行程序来证明这一点,可能需要一段时间。@NyamiouTheGaleanthrope我的缺点是没有给出足够的con文本,num将更改。据我所知,如果计数器等于3,则意味着num==0在3次迭代中为真,而不是num==0被检查了3次。如果条件num==0
在每次迭代中检查一次,则情况也是如此。关于第二次编辑,它试图在num==0为真时调用doSomething,并在平均值中计算它ime,同时如果num==0也计算它是false。您的第二个代码段为每个迭代检查两次num==0
,一次在if
,一次在循环条件中。是的,这是正确的,但我主要关心的是计算while循环的num==0。让我重新表述一下,我正在尝试计算while循环中检查num==0的次数,如果是就像你的第一个代码片段一样,只有当num==0为真时才算。很抱歉,英语不是我的第一语言。while(num==0){if(num==0)
if(num==0)
当它是while(num==0)之后的第一件事时,if(num==0)
有什么意义
哪一个已经检查了该条件?他的目标是查看条件在循环中执行的时间。所以我将计数器放在末尾,因为即使有continue,它也会考虑。
int num = 0;
int counter = 0; // The Overall iteration counter
int doSomethingCounter = 0; // The doSomething condition counter
int doSomeOtherThingCounter = 0; // The doOtherThing condition counter
int ifCounter = 0; // The IF someNum condition counter
int elseIfCounter = 0; // The ELSE IF someNum condition counter
int someNum = 0;
boolean doSomething = true;
String doOtherThing = "yes";
while (num < 10) {
counter++;
if ((doSomethingCounter == doSomethingCounter++) && doSomething == true) {
doSomething = !doSomething;
continue; // Counters can not increment. Goes to next iteration.
}
if ((doSomeOtherThingCounter == doSomeOtherThingCounter++) && doOtherThing.equals("no")) {
break; // Counters can not increment. Exits loop.
}
// Counts the number of times the following IF condition was checked.
if ((ifCounter == ifCounter++) && someNum < 4) {
someNum = doSomethingCool(someNum);
}
// Counts the number of times ELSE IF condition is checked.
else if ((elseIfCounter == elseIfCounter++) && someNum > 0) {
/* increments num by 1 until it reaches 10 then loop
will exit on next iteration attempt. */
num++;
}
}
System.out.println("The WHILE loop has done a total of: " + counter + " iterations.");
System.out.println("The doSomething IF condition was checked: " + doSomethingCounter + " times.");
System.out.println("The doOtherThing IF condition was checked: " + doSomeOtherThingCounter + " times.");
System.out.println("The someNum IF condition was checked: " + ifCounter + " times.");
System.out.println("The someNum ELSE IF condition was checked: " + elseIfCounter + " times.");
The WHILE loop has done a total of: 15 iterations.
The doSomething IF condition was checked: 15 times.
The doOtherThing IF condition was checked: 14 times.
The someNum IF condition was checked: 14 times.
The someNum ELSE IF condition was checked: 10 times.