错误:找不到或加载主类分配5.Assignment5

时间:2019-05-09 04:44:08

标签: java netbeans

我制作了一个程序,该程序可以接收(int)门课程以及(arr)先决条件,并按照正确的顺序进行学习。但是,每当我尝试运行此代码时,都会出现以下错误消息:错误:无法找到或加载主类的assignment5.Assignment5。我不知道如何解决它,并非常感谢您提供一些帮助。预先感谢

package Assignment5;

import java.util.*;

/**
 *
 * @author anon
 */
public class findOrder {

    private Stack<Vertex> vertexStack;

    public findOrder() {
        vertexStack = new Stack<>();
    }

    public static void main(String args[]) {
        int[][] arr = {{1, 0}, {2, 0}, {3, 1}, {3, 2}}; //already sent the code last night during the due date, but changed it slightly so it works a little better
        int rows1 = arr.length;

        int[][] brr = {{1, 0}};
        int rows2 = brr.length;

        int[][] crr = {{1, 0}, {2, 1}, {3, 2}, {4, 3}, {5, 4}};
        int rows3 = crr.length;

        int[][] drr = {{1, 0}, {2, 0}, {3, 1}, {2, 1}, {4, 3}, {5, 3}, {6, 4}, {6, 5}};
        int rows4 = drr.length;

        findOrder dfs1 = new findOrder();
        System.out.println(Arrays.toString(dfs1.findOrder(4, arr)));

        findOrder dfs2 = new findOrder();
        System.out.println(Arrays.toString(dfs2.findOrder(2, brr)));

        findOrder dfs3 = new findOrder();
        System.out.println(Arrays.toString(dfs3.findOrder(6, crr)));

        findOrder dfs4 = new findOrder();
        System.out.println(Arrays.toString(dfs4.findOrder(7, drr)));

    }

    private class Vertex {

        int number;
        boolean visited;
        List<Vertex> neighbours;

        public Vertex(int number) {
            this.number = number;
            this.neighbours = new ArrayList<>();
        }

        public int getNumber() {
            return number;
        }

        public void setVisited(boolean visited) {
            this.visited = visited;
        }

        public void addneighbours(Vertex neighbourVertex) {
            this.neighbours.add(neighbourVertex);
        }

        public List<Vertex> getNeighbours() {
            return neighbours;
        }

        public void setNeighbours(List<Vertex> neighbours) {
            this.neighbours = neighbours;
        }

    }

    public void toplogicalSort(Vertex vertex) {
        List<Vertex> neighbours = vertex.getNeighbours();
        for (int i = 0; i < neighbours.size(); i++) {
            Vertex vertexToGo = neighbours.get(i);
            if (vertexToGo != null && !vertexToGo.visited) {
                toplogicalSort(vertexToGo);
                vertexToGo.setVisited(true);
            }
        }
        vertexStack.push(vertex);
    }

    public int[] findOrder(int numCourses, int[][] prerequisites) {
        int[][] adjacentyMatrix = new int[numCourses][numCourses];

        Vertex[] list = new Vertex[numCourses];

        for (int i = 0; i < numCourses; i++) {
            list[i] = new Vertex(i);
        }
        for (int i = 0; i < prerequisites.length; i++) {
            list[prerequisites[i][1]].addneighbours(list[prerequisites[i][0]]);
        }

        toplogicalSort(list[0]);

        int[] answer = new int[numCourses];

        int j = 0;
        for (int i = answer.length - 1; i >= 0; i--) {
            answer[j++] = vertexStack.get(i).getNumber();
        }
        return answer;

    }
}

2 个答案:

答案 0 :(得分:0)

将Java文件名Assignment5.java更改为findOrder.java文件。 如果要将文件名保留为Assignment5.java,则将其替换为以下代码更改。

import java.util.*;

/**
 *
 * @author anon
 */
public class Assignment5 {

    private Stack<Vertex> vertexStack;

    public Assignment5() {
        vertexStack = new Stack<>();
    }

    public static void main(String args[]) {
        int[][] arr = {{1, 0}, {2, 0}, {3, 1}, {3, 2}}; //already sent the code last night during the due date, but changed it slightly so it works a little better
        int rows1 = arr.length;

        int[][] brr = {{1, 0}};
        int rows2 = brr.length;

        int[][] crr = {{1, 0}, {2, 1}, {3, 2}, {4, 3}, {5, 4}};
        int rows3 = crr.length;

        int[][] drr = {{1, 0}, {2, 0}, {3, 1}, {2, 1}, {4, 3}, {5, 3}, {6, 4}, {6, 5}};
        int rows4 = drr.length;

        Assignment5 dfs1 = new Assignment5();
        System.out.println(Arrays.toString(dfs1.findOrder(4, arr)));

        Assignment5 dfs2 = new Assignment5();
        System.out.println(Arrays.toString(dfs2.findOrder(2, brr)));

        Assignment5 dfs3 = new Assignment5();
        System.out.println(Arrays.toString(dfs3.findOrder(6, crr)));

        Assignment5 dfs4 = new Assignment5();
        System.out.println(Arrays.toString(dfs4.findOrder(7, drr)));

    }

    private class Vertex {

        int number;
        boolean visited;
        List<Vertex> neighbours;

        public Vertex(int number) {
            this.number = number;
            this.neighbours = new ArrayList<>();
        }

        public int getNumber() {
            return number;
        }

        public void setVisited(boolean visited) {
            this.visited = visited;
        }

        public void addneighbours(Vertex neighbourVertex) {
            this.neighbours.add(neighbourVertex);
        }

        public List<Vertex> getNeighbours() {
            return neighbours;
        }

        public void setNeighbours(List<Vertex> neighbours) {
            this.neighbours = neighbours;
        }

    }

    public void toplogicalSort(Vertex vertex) {
        List<Vertex> neighbours = vertex.getNeighbours();
        for (int i = 0; i < neighbours.size(); i++) {
            Vertex vertexToGo = neighbours.get(i);
            if (vertexToGo != null && !vertexToGo.visited) {
                toplogicalSort(vertexToGo);
                vertexToGo.setVisited(true);
            }
        }
        vertexStack.push(vertex);
    }

    public int[] findOrder(int numCourses, int[][] prerequisites) {
        int[][] adjacentyMatrix = new int[numCourses][numCourses];

        Vertex[] list = new Vertex[numCourses];

        for (int i = 0; i < numCourses; i++) {
            list[i] = new Vertex(i);
        }
        for (int i = 0; i < prerequisites.length; i++) {
            list[prerequisites[i][1]].addneighbours(list[prerequisites[i][0]]);
        }

        toplogicalSort(list[0]);

        int[] answer = new int[numCourses];

        int j = 0;
        for (int i = answer.length - 1; i >= 0; i--) {
            answer[j++] = vertexStack.get(i).getNumber();
        }
        return answer;

    }
}

答案 1 :(得分:0)

如果Dhaval的答案对您不起作用,请尝试在项目属性中选择主类。 如何执行将取决于IDE。

例如:

  • 在Intellij中,右键单击项目浏览器中的类文件,然后选择“运行findorder.main()”。
  • 在NetBeans中,右键单击项目并选择属性,然后从“运行”选项卡中选择您的类的名称(Assignment5.findorder)作为“主类:”-您可以使用浏览按钮来帮助输入正确的类和包名称。
  • 在Eclipse中,您可以在类的源文件中选择任何内容,然后单击运行符号。另一种方法是单击运行符号旁边的箭头,然后单击“运行配置”。之后,您可以使用指定的主类创建新的运行配置。
  • 其他工具无疑将具有其他方法。

这是NetBeans项目属性对话框,其中显示了“运行”选项卡。

Main class selection in NetBeans

在我的示例中,程序包是“ hexdump”,而包含主要方法的类是“ HexDumpWithFilter”。