gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 09:54:09 26 4
gpt4 key购买 nike

我设法生成了 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();
}
}

最佳答案

我不确定 ij 变量是否代表您的 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,则它始终是奇数。

关于java - 如何打印正在生成的房屋的 x 和 y 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56117955/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com