我正在尝试创建一个二维坐标数组,每个位置的随机值在 1 到 4 之间。我目前在初始化值时遇到问题。这是我当前的方法代码:
public void createMap(){
for (int i = 1; i < 20; i ++){
for (int j = 1 ; j < 20; j ++) {
coord[i][j] = setCoordinates(random.nextInt(4) + 1, random.nextInt(4) + 1);
}
}
System.out.println(getCoord());
}
这个方法:
public Coordinates setCoordinates (int row, int column){
this.row = row;
this.column = column;
return coord[row][column];
}
和坐标类:
public class Coordinates {
int row;
int column;
public void setColumn(int column){
this.column = column;
}
public void setRow(int row){
this.row = row;
}
public int getRow(){
return row;
}
public int getColumn(){
return column;
}
}
控制台中的结果始终是
null
。
如何更改代码以实际初始化数组中的值?
最终目标是为 2D 游戏创建坐标网格。如果我尝试在 GUI GridPane 上使用它,例如返回类型,我应该记住什么具体内容吗?如果需要更多信息,请告诉我。预先感谢您的帮助。
public Coordinates setCoordinates (int row, int column){
Coordinates c = new Coordinates();
c.setRow(row);
c.setColumn(column);
return c;
}
在这种情况下坐标应该是
Coordinates[][] coord = new Coordinates[x][y];
我无法从您的解释中得到太多信息,但我向您展示了两个带坐标类和不带坐标类的完整示例:
示例 1,使用返回坐标对象的 setCooperative 函数:
package com.company;
import java.io.Console;
import java.util.concurrent.ThreadLocalRandom;
public class Main {
public static void main(String[] args) {
// write your code here
Coordinates[] coord = new Coordinates[20];
createMap();
}
public static void createMap(){
Coordinates[] coord = new Coordinates[20];
for(int i = 0; i < 20; i ++){
coord[i] = setCoordinates(ThreadLocalRandom.current().nextInt(0, 99) + 1, ThreadLocalRandom.current().nextInt(0, 99) + 1);
}
for(int i = 0; i < 20; i ++){
coord[i].getCoord();
}
}
public static Coordinates setCoordinates (int row, int column){
Coordinates c = new Coordinates();
c.setRow(row);
c.setColumn(column);
return c;
}
public static class Coordinates {
int row;
int column;
public Coordinates(){
//constructor
}
public void setColumn(int column){
this.column = column;
}
public void setRow(int row){
this.row = row;
}
public int getRow(){
return row;
}
public int getColumn(){
return column;
}
public void getCoord(){
//just return and print the coordinates
System.out.println("ROW: " + this.getRow() + " COL: " + this.getColumn());
//change void to return and return value if you like :)
}
}
}
如果你愿意,你可以使用 getCoord() 并通过知道 [0] 是行和 [1] 是列来返回坐标数组,如下所示:
public int[] getCoord()
{
int coords[] = new int[2];
coords[0] = this.getRow();
coords[1] = this.getColumn();
return coords;
}
然后你可以这样打印
for(int i = 0; i < 20; i ++){
int coords[] = coord[i].getCoord();
System.out.println(coords[0] + " - " + coords[1]);
}
和示例 2,不返回坐标
package com.company;
import java.io.Console;
import java.util.concurrent.ThreadLocalRandom;
public class Main {
public static void main(String[] args) {
// write your code here
Coordinates[] coord = new Coordinates[20];
createMap();
}
public static void createMap(){
Coordinates[] coord = new Coordinates[20];
for(int i = 0; i < 20; i ++){
coord[i].setCoordinates(ThreadLocalRandom.current().nextInt(0, 99) + 1, ThreadLocalRandom.current().nextInt(0, 99) + 1);
}
for(int i = 0; i < 20; i ++){
coord[i].getCoord();
}
}
public static class Coordinates {
int row;
int column;
public Coordinates(){
//constructor
}
public void setColumn(int column){
this.column = column;
}
public void setRow(int row){
this.row = row;
}
public int getRow(){
return row;
}
public int getColumn(){
return column;
}
public void setCoordinates (int row, int column){
this.setRow(row);
this.setColumn(column);
}
public void getCoord(){
//just return and print the coordinates
System.out.println("ROW: " + this.getRow() + " COL: " + this.getColumn());
//change void to return and return value if you like :)
}
}
}