You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何break语句允许执行sc.nextLine()却忽略后续条件,未完整执行内层for循环?

问题分析与解决方案

核心问题解释

  1. break的作用范围:你代码里的break只会跳出当前的内层for循环,外层的餐厅循环会继续执行。所以当找到同时有pea soup和pancakes的餐厅后,内层循环终止,但外层循环会进入下一次迭代,继续读取下一个餐厅的输入(包括执行String item = sc.nextLine();),这就是你看到该语句仍会执行的原因。

  2. 内层循环提前终止的原因:你在代码里明确写了if (hasRequiredItems[0] && hasRequiredItems[1]) { break; },一旦两个菜品都找到,就会立刻跳出内层循环,自然不会完整执行整个菜单的遍历——这是你自己写的逻辑导致的,和break的特性一致。

代码修正方案

你的需求是找到第一个符合条件的餐厅后就停止所有处理,不需要继续读取后续输入。需要在找到目标餐厅后,同时跳出内层和外层循环,或者用标志位控制循环终止。以下是两种修正方式:

方式一:使用标志位控制外层循环

import java.util.*;

public class PeaImproved {
    public static void main(String[] args) {
        String rightStore = null;
        Scanner sc = new Scanner(System.in);
        int restaurants = sc.nextInt(); 
        sc.nextLine(); 
        boolean found = false; // 新增标志位
        
        for (int i = 0; i < restaurants && !found; i++) { // 加入标志位判断
            int menuLength = sc.nextInt(); 
            sc.nextLine();
            String restaurantName = sc.nextLine(); 
            boolean[] hasRequiredItems = {false, false};

            for (int j = 0; j < menuLength; j++) {
                String item = sc.nextLine();
                
                if (item.equals("pea soup")) {
                    hasRequiredItems[0] = true;
                }
                
                if (item.equals("pancakes")) {
                    hasRequiredItems[1] = true;
                }

                if (hasRequiredItems[0] && hasRequiredItems[1]) {
                    rightStore = restaurantName;
                    found = true; // 标记找到目标
                    break; // 跳出内层循环
                }
            }
        }
        
        if (rightStore != null) {
            System.out.println(rightStore);
        } else {
            System.out.println("Anywhere is fine I guess");
        }
    }
}

方式二:使用带标签的break跳出外层循环

import java.util.*;

public class PeaImproved {
    public static void main(String[] args) {
        String rightStore = null;
        Scanner sc = new Scanner(System.in);
        int restaurants = sc.nextInt(); 
        sc.nextLine(); 
        
        outerLoop: // 给外层循环加标签
        for (int i = 0; i < restaurants; i++) {
            int menuLength = sc.nextInt(); 
            sc.nextLine();
            String restaurantName = sc.nextLine(); 
            boolean[] hasRequiredItems = {false, false};

            for (int j = 0; j < menuLength; j++) {
                String item = sc.nextLine();
                
                if (item.equals("pea soup")) {
                    hasRequiredItems[0] = true;
                }
                
                if (item.equals("pancakes")) {
                    hasRequiredItems[1] = true;
                }

                if (hasRequiredItems[0] && hasRequiredItems[1]) {
                    rightStore = restaurantName;
                    break outerLoop; // 直接跳出外层循环
                }
            }
        }
        
        if (rightStore != null) {
            System.out.println(rightStore);
        } else {
            System.out.println("Anywhere is fine I guess");
        }
    }
}

额外说明

如果你的需求确实是要完整执行内层循环(遍历完整个菜单),那只需要删除内层循环里的break;语句即可。但根据你的问题需求,找到第一个符合条件的餐厅就输出,所以保留终止逻辑更合理,同时要确保外层循环也停止,避免无效的输入读取。

内容的提问来源于stack exchange,提问作者kbkb

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 13:11:02