【问题标题】:Graph Matrix project (non-static in static)图形矩阵项目(静态中的非静态)
【发布时间】:2013-11-11 02:52:24
【问题描述】:

我有一个任务来编写一个图形矩阵,该矩阵用数字标记它们并吐出连接。现在有这个非静态/静态东西的基本问题。即使我认为我了解类和该类的实例之间的区别,我也不明白这个问题。当我运行它时,首先 for 循环出现问题。不会因输入标签而暂停。感谢任何帮助和/或批评。

public class GraphMatrix {

class Matrix {

    private int matrix[][];
    private int size;
    private String labels[];

    private void createMatrix() {
        Scanner in = new Scanner(System.in);
        System.out.println("How many points will be represented in this graph?");
        size = in.nextInt();
        matrix = new int[size][size];
        labels = new String[size];
        System.out.println("Please label each point.");
        for (int i = 1; i <= size; i++) {
            System.out.println("Enter label for point #" + (i));
            String key = in.nextLine();
            labels[i] = key;
        }
        System.out.println("Please define edges between points or enter -1 when finished:");
        int finish = 1;
        while (finish == 1) {
            int jkey = 0;
            int kkey = 0;
            int count = 0;
            boolean pass = false;
            System.out.println("Point labeled:");
            String j = in.nextLine();
            while (pass = false) {
                if (labels[count].equals(j)) {
                    jkey = count;
                    count = 0;
                    pass = true;
                }
            }
            System.out.println("to point labeled:");
            String k = in.nextLine();
            while (pass = true) {
                if (labels[count].equals(j)) {
                    kkey = count;
                    pass = false;
                }
            }
            matrix[jkey][kkey] = 1;
            System.out.println("Finished enter -1, to define more connections enter 1");
            finish = in.nextInt();
        }

    }

    private void listEdges() {
        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                if (matrix[i][j] == 1) {
                    System.out.println("There is an edge between" + labels[i] + " and" + labels[j]);
                }
            }
        }
    }
}

public static void main(String[] args) {
    Matrix neo = new Matrix();
    neo.createMatrix();
    neo.listEdges();
}

}

【问题讨论】:

  • 如果您的编译器给您任何错误,请在此处发布并告诉我们是哪一行引起的。
  • 在浏览了您的代码之后,我没有看到任何关于静态与非静态问题的问题。正如气垫船所说,请发布您的错误。
  • 我同意@SteveP。看起来您的标题与您显示的代码完全无关。请澄清。
  • 或至少pass == false。几行之后pass = true 也出现了类似的问题。应该只是while(pass)while(pass == true)
  • 编译没有问题。我的问题出在第一个 for 循环中,它不等待输入,所以我得到: 输入点 #1 的标签 输入点 #2 的标签 不等待标签 #1 的输入

标签: java graph matrix


【解决方案1】:

您需要在 ma​​in 方法中进行如下更改以消除编译器错误。

来自

 Matrix neo = new Matrix();

  GraphMatrix graphMatrix = new GraphMatrix();
    Matrix neo = graphMatrix.new Matrix();

注意:要实例化内部类,必须先实例化外部类。然后,使用以下语法在外部对象中创建内部对象:

 OuterClass.InnerClass innerObject = outerObject.new InnerClass();

【讨论】:

    【解决方案2】:

    我在您的代码中发现了一个重大问题。

    这个while (pass = false) {应该是while (!pass) {

    while (pass = true) { 应该是while (pass) {

    另外:为什么要将 Matrix 类嵌套在 GraphMatrix 类中?这对我来说没有意义。如果我是你,除非你的任务明确要求,否则我不会这样做。

    【讨论】:

    • 可能值得注意的是,他在几行之后使用while(pass = true) 遇到了同样的问题。 while(pass == true) 或简单的while(pass),两者都可以接受,但单个=while 中都有问题。
    猜你喜欢
    • 2014-05-18
    • 1970-01-01
    • 2018-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-20
    • 1970-01-01
    • 2017-09-17
    相关资源
    最近更新 更多