【问题标题】:for while if loop nested error message if nothing foundfor while if 如果没有找到,则循环嵌套错误消息
【发布时间】:2017-12-11 03:34:27
【问题描述】:
                String aaa= "password";
                int wloop = 0;
                while (wloop<myarraylist.size()) {

                    for (int i = 0; i < myarraylist.size(); i++) {
                        Something qwerty = myarraylist.get(i);
                        String bbb= qwerty.getinfor();

                        if (aaa.equals(bbb)) {
                            System.out.println("you have found it!");
                            wloop = myarraylist.size();

                        }wloop++;
                    }
                    System.out.println("nothing have been found");
                }

我有一个要使用 for 循环迭代的东西的数组列表。每次迭代时,我都会从数组列表中获取一个对象。我以字符串格式获取它的信息。如果我在对象信息中找到字符串“密码”,我想在屏幕上打印一条消息,说“我已经创建了它”。

你可能会认为我只会找到字符串“密码”一次。

但是,如果在遍历整个数组列表之后,我希望在屏幕上打印“没有找到任何东西”的消息。

不过

正如目前所写的那样,无论发生什么,“什么都没有找到”总是会打印到屏幕上。我无法在 for 循环中不发生任何事情,在每次迭代之后(每个迭代都由 if 语句检查)成为触发动作的条件。我尝试将整个 for 循环放在 if 语句中,但在某处添加“return true”时遇到了麻烦。

【问题讨论】:

  • 设置wloop = arraylist.size() 强制循环退出看起来你正试图“欺骗”循环退出。当你可以使用简单的代码来明确你在做什么时,不要使用棘手的代码。在这种情况下,break.
  • 谢谢!当我们学习循环时,我们没有被告知课堂休息时间。现在我知道了。

标签: java for-loop if-statement while-loop


【解决方案1】:

在 Java 8+ 中,您可以使用 StreamanyMatch 来确定是否匹配。喜欢,

if (myarraylist.stream().anyMatch(qwerty -> aaa.equals(qwerty.getinfor()))) {
    System.out.println("you have found it");
} else {
    System.out.println("nothing found");
}

在早期版本中,您需要一个标志。而且您不需要嵌套循环,但我更喜欢for-each 循环。类似的,

boolean found = false;
for (Something qwerty : myarraylist) {
    if (aaa.equals(qwerty.getinfor())) {
        found = true;
        break;
    }
}
if (found) {
    System.out.println("you have found it");
} else {
    System.out.println("nothing found");
}

【讨论】:

    【解决方案2】:

    您可以使用标志来设置未找到密码的状态,并根据该状态打印该语句。也许是这样的:

                String aaa= "password";
                int wloop = 0;
                boolean isPasswordFound = false;
                while (wloop<myarraylist.size()) {
    
                    for (int i = 0; i < myarraylist.size(); i++) {
                        Something qwerty = myarraylist.get(i);
                        String bbb= qwerty.getinfor();
    
                        if (aaa.equals(bbb)) {
                            System.out.println("you have found it!");
                            isPasswordFound = true;
                            wloop = myarraylist.size();
    
                        }wloop++;
                    }
                    if(!isPasswordFound) {
                        System.out.println("nothing have been found");
                    }
    
                }
    

    【讨论】:

    • 这可能是一个解决方案。但是我们真的需要两个循环吗?
    • @ShubhenduPramanik 只提供解决方案,而不是做整个优化。
    • 感谢您的帮助。任何观点都是有帮助的,因为我是 9 月份的新计算机科学专业学生。了解做某事的多种方式很有帮助。我现在得睡觉了。但我赞成你的两个答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-21
    • 2019-09-26
    • 1970-01-01
    • 2015-07-06
    • 1970-01-01
    • 2020-12-30
    相关资源
    最近更新 更多