我正在尝试制作一个 for 循环,它使我有 1000 个对象并将它们放置到随机生成的点 (x, y)。所以这里是代码。我一直在努力解决这么多小时,我也一直在网上搜索,但还没有找到任何方法来做到这一点。在那个循环之后,我尝试将这些对象添加到某种雷达中。
这是代码(所以问题是我不知道如何从循环中获取变量并使其出现在循环之外):
case "look": {
System.out.print("You are at: " +px +", " +py);
System.out.println("");
StringBuilder objects = new StringBuilder(); //That's something i found out form the net..
while (objnum>=0){ objnum--; //Creates randomly 1000objects around the map
int objid = (int)(Math.random() * 11 + 1); //int objnum is 1000, told above
int objx = (int)(Math.random() * 10000 + 1);
int objy = (int)(Math.random() * 10000 + 1);}
board.spawnObject(new BoardObject(objectid, objx, //That's something i found out form the net..
objy, objnum));
for(int x=px-2 ; x< px+3 ; x++ ){ //px=player position
for(int y=py-2 ; y< py+3 ; y++ ){ //this is how radar is created
if(objid==1 && x==objx && y==objy){board[x][y]=1;}
else if(objid==2 && x==objx && y==objy){board[x][y]=2;}
else if(objid==3 && x==objx && y==objy){board[x][y]=3;} //That's where i need info from the loop..
else if(objid==4 && x==objx && y==objy){board[x][y]=4;}
else if(objid==5 && x==objx && y==objy){board[x][y]=5;}
else if(objid==6 && x==objx && y==objy){board[x][y]=-1;}
else if(objid==7 && x==objx && y==objy){board[x][y]=-2;}
else if(objid==8 && x==objx && y==objy){board[x][y]=-3;}
else if(objid==9 && x==objx && y==objy){board[x][y]=-4;}
else if(objid==10 && x==objx && y==objy){board[x][y]=-5;}
if(x==px && y==py){
board[x][y]=6;}//<- this shows players position on radar
if(board[x][y]==-1){
System.out.print("[sto]");
}else if(board[x][y]==0){
System.out.print("[___]");//<- This works well..
}else if(board[x][y]==-2){
System.out.print("[box]");
}
else if(board[x][y]==-3){
System.out.print("[ppl]");
}
else if(board[x][y]==-4){
System.out.print("[pit]");
}
else if(board[x][y]==-5){
System.out.print("[brk]");
} //That's how radar shows dots/objects
else if(board[x][y]==1){
System.out.print("[kid]");
}
else if(board[x][y]==2){
System.out.print("[tre]");
}
else if(board[x][y]==3){
System.out.print("[pet]");
}
else if(board[x][y]==4){
System.out.print("[bus]");
}
else if(board[x][y]==5){
System.out.print("[???]");
}
else if(board[x][y]==6){
System.out.print("[You]");} //<- This works well..
}
System.out.println();
}; }break;