【问题标题】:Basic else statement in array for loop being neglected because of continue由于 continue 而忽略了数组 for 循环中的基本 else 语句
【发布时间】:2015-10-22 09:43:31
【问题描述】:

我试图了解我的 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 块,代码只会检查第一次迭代。

谢谢你们的时间。

【问题讨论】:

  • 循环中的逻辑没有意义。第一个if 表示if (myA[u]==toS) { ... break; }。所以只有当myA[u] 不等于toS 时,迭代才能继续。很明显,else 子句永远不会触发。

标签: java arrays for-loop continue


【解决方案1】:

您不应在循环中使用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");
    }

【讨论】:

    【解决方案2】:

    你的 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;
        }
    

    请注意,只有在循环结束后,您才能检查是否找到了该项目。

    【讨论】:

    • 感谢您的精彩解释。我能够根据您的解释解决我正在处理的 gui 工具项目中使用的代码的其他部分。我陷入了数组的这个问题,无法摆脱 ti,你的例子让我有了更好的洞察力。
    【解决方案3】:

    看下面的sn-p

      if(myA[u] == toS){
             System.out.println("Item found" );
             break;
        }
       if(myA[u] != toS){
                        continue;
       }
    

    在任何可能的情况下,toS 将等于 myA[u] 或不等于它,

    如果相等,则执行'break'语句并退出for循环, 如果不相等,则执行“继续”语句,因此退出正常的迭代循环

    所以你的 else 循环将永远不会运行,因为没有条件 2 个 if 循环之一不会执行

    希望这会有所帮助!

    祝你好运!

    【讨论】:

      猜你喜欢
      • 2013-05-11
      • 1970-01-01
      • 2023-03-21
      • 2016-08-09
      • 2015-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多