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

Java排列生成代码在zyBooks编译报错:找不到printAllPermutations方法

问题修复方案

错误原因

zyBooks的测试代码尝试调用printAllPermutations方法,但你的PhotoLineups类里定义的方法名是allPermutations,方法名不匹配导致编译器找不到该方法,触发编译错误。

修复步骤

将你类中的allPermutations方法名修改为printAllPermutations,确保和测试代码的调用一致。

修改后的完整代码

import java.util.Scanner;
import java.util.ArrayList;

public class PhotoLineups {
    public static void printAllPermutations(ArrayList<String> permList, ArrayList<String> nameList) {    
        if (nameList.isEmpty()) {
            for (int i = 0; i < permList.size(); i++) {
                if(i == permList.size()-1) {
                    System.out.print(permList.get(i));
                }
                else {
                    System.out.print(permList.get(i)+", ");
                }
            }
            System.out.println();
        } else {
            for (int i = 0; i < nameList.size(); ++i) {
                ArrayList<String> newPerm = new ArrayList<String>(permList);
                newPerm.add(nameList.get(i));
                ArrayList<String> newNameList = new ArrayList<String>(nameList);
                newNameList.remove(i);
                printAllPermutations(newPerm, newNameList);
            }
        }
    }
    public static void main(String[] args) {
        Scanner scnr = new Scanner(System.in);
        ArrayList<String> nameList = new ArrayList<String>();
        ArrayList<String> permList = new ArrayList<String>();
        String name;
        while (true) {
            name = scnr.next();
            if (name.equals("-1")) break;
            nameList.add(name);
        }

        printAllPermutations(permList, nameList);
    }
}

额外说明

修改后,测试代码就能找到对应的方法,编译错误会消失。同时注意保持方法的参数列表、修饰符(public static)和原方法一致,确保功能不受影响。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.25 22:39:25