【问题标题】:Break multiple for loops in java when a condition is met Java满足条件时在Java中打破多个for循环Java
【发布时间】:2021-09-21 00:06:57
【问题描述】:

我有四个String[],其中包含一些单词并且长度不同。这些关键字在 txt 文件中进行搜索,如果找到一个单词,循环就会中断。问题是如果在第一个数组中找到单词,我该如何停止其他循环?就像如果在第一个数组中找到这个词,我希望另一个 for 循环中断,否则如果找不到这个词,那么我希望第二个循环进行迭代。我在嵌套 for 循环中尝试过,但我没有为我工作,因为我没有使用嵌套 for 循环。

这是我的代码:

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class practice {

static String wfound;

public static void main(String[] args) throws IOException {
    String a[] = { "universe", "world", "html" ,"name"};
    String b[] = { "animal", "Bird", "HTML" };
    String c[] = { "choclate", "HTML", "hello" };
    String d[] = { "css", "side"};

    String file= new String(Files.readAllBytes(Paths.get("D://test1.txt")), StandardCharsets.UTF_8);

    for (String an : a) {
        Pattern p = Pattern.compile("\\b" + an + "\\b", Pattern.CASE_INSENSITIVE);
        Matcher m = p.matcher(file);
        if (m.find()) {
            wfound = an;
            System.out.println("In array 1: " + an);
            break;
        }
    }
    for (String ab : b) {
        Pattern p = Pattern.compile("\\b" + ab + "\\b", Pattern.CASE_INSENSITIVE);
        Matcher m = p.matcher(file);
        if (m.find()) {
            wfound = ab;
            System.out.println("In array 2: " + ab);
            break;
        }
    }
    for (String ac : c) {
        Pattern p = Pattern.compile("\\b" + ac + "\\b", Pattern.CASE_INSENSITIVE);
        Matcher m = p.matcher(file);
        if (m.find()) {
            wfound = ac;
            System.out.println("In array 3: " + ac);
            break;
        }
    }
    for (String ad : d) {
        Pattern p = Pattern.compile("\\b" + ad + "\\b", Pattern.CASE_INSENSITIVE);
        Matcher m = p.matcher(file);
        if (m.find()) {
            wfound = ad;
            System.out.println("In array 4: " + ad);
            break;
        }
    }

}

}

【问题讨论】:

  • 有几个选项,但我看到的最 OOP 方式是责任链模式。至少,将其拆分为单独的方法。也许试试String[][]
  • 您可以创建一个布尔值并将其设置为 false,如果您的字符串匹配,则将其设置为 true 并在每次进入新循环之前检查它。
  • @SaatvikRamani 所以我必须将这些循环包装到 while 循环中?
  • 只使用二维数组。
  • @john - 不。只需将 for (...) 重写为 if (!flag) for(...),除了 he first 之外的所有循环。

标签: java arrays loops for-loop


【解决方案1】:

只需使用return 代替break

   public class practice {

    static String wfound;

    public static void main(String[] args) throws IOException {
        String a[] = { "universe", "world", "html" ,"name"};
        String b[] = { "animal", "Bird", "HTML" };
        String c[] = { "choclate", "HTML", "hello" };
        String d[] = { "css", "side"};

        String file= new String(Files.readAllBytes(Paths.get("D://test1.txt")), StandardCharsets.UTF_8);

        for (String an : a) {
            Pattern p = Pattern.compile("\\b" + an + "\\b", Pattern.CASE_INSENSITIVE);
            Matcher m = p.matcher(file);
            if (m.find()) {
                wfound = an;
                System.out.println("In array 1: " + an);
                return;
            }
        }
        for (String ab : b) {
            Pattern p = Pattern.compile("\\b" + ab + "\\b", Pattern.CASE_INSENSITIVE);
            Matcher m = p.matcher(file);
            if (m.find()) {
                wfound = ab;
                System.out.println("In array 2: " + ab);
                return;
            }
        }
        for (String ac : c) {
            Pattern p = Pattern.compile("\\b" + ac + "\\b", Pattern.CASE_INSENSITIVE);
            Matcher m = p.matcher(file);
            if (m.find()) {
                wfound = ac;
                System.out.println("In array 3: " + ac);
                return;
            }
        }
        for (String ad : d) {
            Pattern p = Pattern.compile("\\b" + ad + "\\b", Pattern.CASE_INSENSITIVE);
            Matcher m = p.matcher(file);
            if (m.find()) {
                wfound = ad;
                System.out.println("In array 4: " + ad);
                return;
            }
        }

    }
}

【讨论】:

  • 这太复杂了。 if (isFound) 检查没有理由后续循环中。如果在设置标志时您要做的只是返回,那么您不妨省略标志,并在找到匹配项时返回。
  • 是的,你是对的,当你写评论时,我正在编辑我的答案。谢谢)
  • @DmitriiBykov 有什么方法可以访问在我的主函数中存储单词的 wfound 变量。我在课堂上声明它是静态的,但我仍然无法访问它
  • 在这种情况下,您无法访问它,因为当您找到匹配项时程序已完成 - 因为它从“main”返回。但这正是您原始帖子所暗示的效果;如果你跳过后续的循环,那么在程序结束之前什么都不做。
【解决方案2】:

代码重复太多,不符合我的口味。我会先分解出常见的处理:

String findAndPrint(String[] arr, String file, int n) {
    for (String an : arr) {
        Pattern p = Pattern.compile("\\b"+an+"\\b", Pattern.CASE_INSENSITIVE);
        Matcher m = p.matcher(file);
        if (m.find()) {;
            System.out.printf("In array %d: %s%n", n, an);
            return an;
        }
    }
    return null;
}

然后,在 main 中,你可以写:

if (findAndPrint(a, file, 1) != null)
   return;
if (findAndPrint(b, file, 2) != null)
   return;
if (findAndPrint(c, file, 3) != null)
   return;
if (findAndPrint(d, file, 4) != null)
   return;

甚至

String[][] s = { a, b, c, d };
for (int i=0; i<s.length; i++){
   String wfound = findAndPrint(s[i], file, i);
   if (wfound != null) {
       // TODO: process string 'wfound' here
       return;
   }
}

在第二次重写中,单独的方法不是“需要的”,但我认为它仍然有助于清晰。

如果示例代码在找到匹配项后需要更多工作,您可以(我确定)相应地进行更改。

关键点是:不要重复代码。

【讨论】:

  • 有什么方法可以在我的主函数中访问存储单词的 wfound 变量。我在课堂上声明它是静态的,但我仍然无法访问它
  • 改变'findAndPrint'的返回类型,如果找到则返回字符串,如果没有找到则返回null。
  • 我将变量 return true 和 false 改为 null 但我仍然无法访问变量
  • 我希望你能自己弄清楚,但没关系。完毕。对于它的价值,您不想“访问变量”(在这种情况下是子例程的本地变量),而是使用返回值。 (您可以将其存储在调用例程中的某个变量中,就像我在这里所做的那样)。
猜你喜欢
  • 2010-11-06
  • 1970-01-01
  • 2020-04-21
  • 1970-01-01
  • 2016-01-20
  • 1970-01-01
  • 1970-01-01
  • 2012-02-21
  • 1970-01-01
相关资源
最近更新 更多