由于 continue,for 循环数组中的基本 else 语句被忽略
Basic else statement in array for loop being neglected because of continue
我想了解为什么我的 else 语句没有在这个简单的程序中被访问,基本上,我想做的是循环遍历一个随机数数组,如果找到该项目,文本将显示它已被找到。如果还没有找到,我会继续进行下一次迭代。如果根本没有找到,则会跳出一条声明,说明在数组中找不到该项目。
import java.util.*;
public class practice {
public static void main(String[] args) {
int myA[] = new int[11];
int toS = 59;
Random f = new Random();
for(int j = 0; j < myA.length; j++){
myA[j] = f.nextInt(10);
}
System.out.println("The array has");
for(int x = 0; x < myA.length; x++){
System.out.println(myA[x]);
}
System.out.println("Loop through elements");
System.out.println("loop test");
for(int u = 0; u < myA.length; u++){
if(myA[u] == toS){
System.out.println("Item found" );
break;
}
if(myA[u] != toS){
continue;
}
else{
System.out.println("Item not in list");
break;
}
}// end of loop
}//After this no more code
}
我是不是想多了?我使用调试器来逐步执行这些过程,我看到 continue 语句是从循环中跳出并在没有传递给 else 语句的情况下结束的。我将如何修复迭代?如果我删除 continue 块,代码将只检查第一次迭代。
谢谢大家抽出时间。
你的 else 语句没有进入,因为它已经被前面的 if 语句覆盖,从循环中跳出:
if(myA[u] == toS){
System.out.println("Item found" );
break;
}
if(myA[u] != toS){
continue;
}
else{ // myA[u] == toS - never happens, since previous if statement broke from the loop
System.out.println("Item not in list");
break;
}
你的逻辑可以这样处理,使用一个布尔变量:
boolean found = false;
for(int u = 0; u < myA.length; u++){
if(myA[u] == toS){
System.out.println("Item found" );
found = true;
break;
}
}
if (!found) {
System.out.println("Item not in list");
break;
}
请注意,只有在循环结束后,您才能检查是否找到该项目。
你不应该在循环中使用 continue
。
boolean itemFound = false;
for(int u = 0; u < myA.length; u++){
if(myA[u] == toS){
itemFound = true;
break;
}
}// end of loop
if (itemFound) {
System.out.println("Item found" );
} else {
System.out.println("Item not in list");
}
看看下面的片段
if(myA[u] == toS){
System.out.println("Item found" );
break;
}
if(myA[u] != toS){
continue;
}
在任何可能的情况下,要么 toS 等于 myA[u],要么不等于它,
如果它相等,那么当 'break' 语句被执行并且它从 for 循环中出来时,
如果它不等于 'continue' 语句被执行,所以它从正常的迭代循环中出来
所以你的 else 循环永远不会 运行 ,因为没有条件 2 个 if 循环之一不会执行
希望对您有所帮助!
祝你好运!
我想了解为什么我的 else 语句没有在这个简单的程序中被访问,基本上,我想做的是循环遍历一个随机数数组,如果找到该项目,文本将显示它已被找到。如果还没有找到,我会继续进行下一次迭代。如果根本没有找到,则会跳出一条声明,说明在数组中找不到该项目。
import java.util.*;
public class practice {
public static void main(String[] args) {
int myA[] = new int[11];
int toS = 59;
Random f = new Random();
for(int j = 0; j < myA.length; j++){
myA[j] = f.nextInt(10);
}
System.out.println("The array has");
for(int x = 0; x < myA.length; x++){
System.out.println(myA[x]);
}
System.out.println("Loop through elements");
System.out.println("loop test");
for(int u = 0; u < myA.length; u++){
if(myA[u] == toS){
System.out.println("Item found" );
break;
}
if(myA[u] != toS){
continue;
}
else{
System.out.println("Item not in list");
break;
}
}// end of loop
}//After this no more code
}
我是不是想多了?我使用调试器来逐步执行这些过程,我看到 continue 语句是从循环中跳出并在没有传递给 else 语句的情况下结束的。我将如何修复迭代?如果我删除 continue 块,代码将只检查第一次迭代。
谢谢大家抽出时间。
你的 else 语句没有进入,因为它已经被前面的 if 语句覆盖,从循环中跳出:
if(myA[u] == toS){
System.out.println("Item found" );
break;
}
if(myA[u] != toS){
continue;
}
else{ // myA[u] == toS - never happens, since previous if statement broke from the loop
System.out.println("Item not in list");
break;
}
你的逻辑可以这样处理,使用一个布尔变量:
boolean found = false;
for(int u = 0; u < myA.length; u++){
if(myA[u] == toS){
System.out.println("Item found" );
found = true;
break;
}
}
if (!found) {
System.out.println("Item not in list");
break;
}
请注意,只有在循环结束后,您才能检查是否找到该项目。
你不应该在循环中使用 continue
。
boolean itemFound = false;
for(int u = 0; u < myA.length; u++){
if(myA[u] == toS){
itemFound = true;
break;
}
}// end of loop
if (itemFound) {
System.out.println("Item found" );
} else {
System.out.println("Item not in list");
}
看看下面的片段
if(myA[u] == toS){
System.out.println("Item found" );
break;
}
if(myA[u] != toS){
continue;
}
在任何可能的情况下,要么 toS 等于 myA[u],要么不等于它,
如果它相等,那么当 'break' 语句被执行并且它从 for 循环中出来时, 如果它不等于 'continue' 语句被执行,所以它从正常的迭代循环中出来
所以你的 else 循环永远不会 运行 ,因为没有条件 2 个 if 循环之一不会执行
希望对您有所帮助!
祝你好运!