英文:
Does continue statement blocks the entire code below it, even if it is within if statement?
问题
这是我的代码。
public class TwinPrimeNumbers {
public static void main(String[] args) {
// TODO Auto-generated method stub
boolean isTwinPrime = false;
int count = 0, countTwo = 0, countThree = 0;
inner:
for (int i = 1; i <= 100; i++) {
for (int j = 1; j <= ((i + 2) / 2); j++) {
if (i % j == 0)
count++;
if ((i + 2) % j == 0)
countTwo++; // 1
}
if (count >= 1 || countTwo >= 1) { // This part is having a problem.
continue inner;
}
else {
for (int j = 1; j <= ((i + 1) / 2); j++) {
if ((i + 1) % j == 0) {
countThree++;
}
}
}
isTwinPrime = (i + 2) - i == 2;
if ( isTwinPrime == true && countThree >= 2 && count == 1 && countTwo == 1)
System.out.printf("Twin Primes: ( %d, %d ) %n", i, (i + 2));
count = 0;
countTwo = 0;
countThree = 0;
}
}
}
当这个语句出现在我的代码中时,我遇到了问题。在控制台中没有输出。continue
语句是否会阻止下面的代码运行?
if (count >= 1 || countTwo >= 1) {
continue inner;
}
因为当我运行程序时,没有输出出现。
英文:
Here is my code.
public class TwinPrimeNumbers {
public static void main(String[] args) {
// TODO Auto-generated method stub
boolean isTwinPrime = false;
int count = 0, countTwo = 0, countThree = 0;
inner:
for (int i = 1; i <= 100; i++) {
for (int j = 1; j <= ((i + 2) / 2); j++) {
if (i % j == 0)
count++;
if ((i + 2) % j == 0)
countTwo++; // 1
}
if (count >= 1 || countTwo >= 1) { // This part is having a problem.
continue inner;
}
else {
for (int j = 1; j <= ((i + 1) / 2); j++) {
if ((i + 1) % j == 0) {
countThree++;
}
}
}
isTwinPrime = (i + 2) - i == 2;
if ( isTwinPrime == true && countThree >= 2 && count == 1 && countTwo == 1)
System.out.printf("Twin Primes: ( %d, %d ) %n", i, (i + 2));
count = 0;
countTwo = 0;
countThree = 0;
}
}
}
I'm having a problem when this statement appear on my code There is no output appearing in the console.
Does the continue statement blocks the other code below?
**if (count >= 1 || countTwo >= 1) {
continue inner;
}**
Because there is no output appearing when I run the program.
答案1
得分: 1
continue
语句跳过了循环的当前迭代。
在你的代码中,对于每个 i
的值,count >= 1 || countTwo >= 1
都为真,因此 continue
语句在外部循环的每次迭代中执行,一旦 continue
语句执行,它就会立即跳回循环的开头,而不会执行下面的语句。最终,循环的终止条件计算为假,因此循环终止。
在你的情况下,标签完全没有用,因为没有标签,你的代码将以与带标签的代码完全相同的方式执行。
此外,你的逻辑是错误的。你可以简化你的代码如下:
public class TwinPrimeNumbers {
static boolean isPrime(int n) {
if (n <= 1) return false;
for (int i = 2; i < n; i++)
if (n % i == 0) return false;
return true;
}
public static void main(String args[]) {
int primeOne = -1;
for (int i = 1; i <= 100; i++) {
// 如果当前数字是素数,且前一个保存的素数与当前数字的差为2,
// 那么我们有一对孪生素数
if (i - primeOne == 2 && TwinPrimeNumbers.isPrime(i)) {
System.out.printf("孪生素数: ( %d, %d ) %n", primeOne, i);
// 更新 primeOne
primeOne = i;
}
// 如果前一个条件不成立,且当前数字是素数,
// 则将其保存在 primeOne 变量中
else if (TwinPrimeNumbers.isPrime(i)) {
primeOne = i;
}
}
}
}
输出:
孪生素数: ( 3, 5 )
孪生素数: ( 5, 7 )
孪生素数: ( 11, 13 )
孪生素数: ( 17, 19 )
孪生素数: ( 29, 31 )
孪生素数: ( 41, 43 )
孪生素数: ( 59, 61 )
孪生素数: ( 71, 73 )
英文:
continue
statement skips the current iteration of the loop.
In your code, count >= 1 || countTwo >= 1
is true for each value of i
, so continue
statement executes in each iteration of the outer loop and as soon as continue
statement executes, it jumps back to the start of the loop without ever executing the statements below. Eventually, loop's termination condition evaluates to false and hence the loop breaks.
There is absolutely no use of label in your case because without label, your code will execute in exactly the same manner as it is with the label.
Also your logic is all wrong. You can simplify your code as shown below:
public class TwinPrimeNumbers {
static boolean isPrime(int n) {
if (n <= 1) return false;
for (int i = 2; i < n; i++)
if (n % i == 0) return false;
return true;
}
public static void main(String args[]) {
int primeOne = -1;
for (int i = 1; i <= 100; i++) {
// if current number is prime and the differene between
// previously saved prime number and current number is 2,
// then we have a pair of Twin Primes
if (i - primeOne == 2 && TwinPrimeNumbers.isPrime(i)) {
System.out.printf("Twin Primes: ( %d, %d ) %n", primeOne, i);
// update primeOne
primeOne = i;
}
// if previous condition is false and current number is prime,
// save it in primeOne variable
else if (TwinPrimeNumbers.isPrime(i)) {
primeOne = i;
}
}
}
}
output:
Twin Primes: ( 3, 5 )
Twin Primes: ( 5, 7 )
Twin Primes: ( 11, 13 )
Twin Primes: ( 17, 19 )
Twin Primes: ( 29, 31 )
Twin Primes: ( 41, 43 )
Twin Primes: ( 59, 61 )
Twin Primes: ( 71, 73 )
答案2
得分: 0
continue inner
意味着无论这个语句位于何处(除了只能出现在标记为 inner
的循环内部),都会从标记为 inner
的循环的下一次迭代继续执行。
英文:
continue inner
means to continue execution from the next iteration of the loop labeled inner
, regardless of where this statement is (other than that it may appear only inside said loop).
答案3
得分: 0
你好,欢迎来到StackOverflow!
continue
作为 goto
的使用情况并不被鼓励,因为它代表了对代码的无条件跳转。
在这种情况下,你可以忽略标签,考虑到你的 continue
语句不包含在多个循环中。代码变为:
int count = 0, countTwo = 0, countThree = 0;
for (int i = 1; i <= 100; i++) {
for (int j = 1; j <= ((i + 2) / 2); j++) {
if (i % j == 0)
count++;
if ((i + 2) % j == 0)
countTwo++; // 1
}
if (count >= 1 || countTwo >= 1) { // 这部分有问题。
continue;
}
...
英文:
Hello and welcome to StackOverflow!
continue
's use case as a goto
is not really encouraged as it represents an unconditional jump through the code.
In this case you can discard the label considering your continue
statement is not included in more than one loop. The code becomes:
int count = 0, countTwo = 0, countThree = 0;
for (int i = 1; i <= 100; i++) {
for (int j = 1; j <= ((i + 2) / 2); j++) {
if (i % j == 0)
count++;
if ((i + 2) % j == 0)
countTwo++; // 1
}
if (count >= 1 || countTwo >= 1) { // This part is having a problem.
continue;
}
...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论