如何打印正在生成的房屋的x和y值?

时间:2019-05-13 18:27:29

标签: java multidimensional-array

我设法生成了1000 x 1000的房屋网格,但是它打印出了1000 x 1000的空网格。我希望该网格打印出每个房子的x和y值的1000 x 1000网格。但是,使用我的getx()和gety()方法只会返回NullPointerException。我不确定getx()和gety()方法有什么问题。

public class House {
    static String[] houseletters = Stream.of("A", "B", "C", "D", "E", "F", "G", "H", "I", "J").toArray(String[]::new); 
    static String[] houseletters1 = Stream.of("AA", "BB", "CC", "DD", "EE", "FF", "GG", "II", "JJ").toArray(String[]::new);
    private double streetnum;
    private double avnum;
    private String houseletter = "";
    private double length = 100;
    private double width = 100;
    private double x;
    private double y;
    private double units = 1;

    public House(double streetnum, double avnum, String houseletter, double length, double width, double x, double y, double units) {
        this.streetnum = streetnum;
        this.avnum = avnum;
        this.houseletter = houseletter;
        this.length = length;
        this.width = width;
        this.x = x;
        this.y = y;
        this.units = units;
    }
    public double getStreetNum() {
        return streetnum;
    }
    public double getAvNum() {
        return avnum;
    }
    public String getHL() {
        return houseletter;
    }
    public double getLength() {
        return length;
    }
    public double getWidth() {
        return width;
    }
    public double getX(double x) {
        return x;
    }
    public double getY(double y) {
        return y;
    }
    @Override
    public String toString() {
        return this.houseletter;
    } 
}
    public static void main(String[] args) throws FileNotFoundException {
        House[][] houses = new House[250][100];
        Homerville homerville = new Homerville(2000000, 250, 50, 200, 1000, 24960,"Bart Complex", "Lisa Complex", "Distribution Center", 1, 300, 500);
        Point[][] graph = new Point[1000][1000];
         /*for (int i = 0; i < graph.length; i++) {
            for (int j = 0; j < graph.length;  j++) {
                graph[i][j] = new Point(i ,j);
            }
        }
        */
        printhouses(houses);

    }
    public static void printhouses(House[][] h) {
        for (int i = 0; i < h.length; i++) {
            for (int j = 0; j < h[i].length; j++) {
                System.out.print(h[i][j] + " ");
            }
            System.out.println();
        }
    }

2 个答案:

答案 0 :(得分:1)

我不确定 i j 变量是否代表您的y值,但是您可以使用模运算符来区分偶数/奇数。

if (j % 2 == 1) {
  //odd
  houses[i][j] = new House(i, j, houseletters[j], 100, 100, i, j, 1);
} else {
  //even
  houses[i][j] = new House(i, j, houseletters1[j], 100, 100, i, j, 1);
}

模运算符在除法后返回余数。如果除以2后的余数等于1,则始终为奇数。

答案 1 :(得分:0)

为了解决该问题,我使用了一种称为打印室的单独方法:

public static void printhouses(House[][] h) {
    for (int i = 0; i < h.length; i++) {
        for (int j = 0; j < h[i].length; j++) {
            System.out.print(h[i][j] + " ");
        }
        System.out.println();
    }
}

只是将房子打印为空。我正在尝试使用getX()和getY()方法返回生成的房屋的x和y值,但这给了我NullPointerException。