我正在学习Java,并为我的期末考试做游戏项目。我试图给我的数组添加不同的值,但当我打印数组时,每次它在地图上显示不同的对象时,我将在这里共享输出。这是主类:
import MAP.*;
导入人员*;
Map map = new Map();
Scanner sc = new Scanner(System.in);
Person character = new Person("ahmet");
Person npc = new Person("mehmet");
npc.placeObject();
character.setPosX(character.random1);
character.setPosY(character.random2);
character.placeCharacter(character.posX, character.posY);
System.out.println(character.name);
for (int i = 0 ; i <5 ;i++) {
String a = sc.nextLine();
character.MoveCharacter(a);
}
System.out.println("\n");
}
}
public int posX;
public int posY;
public int getPosX(Random rnd){
return posX;
}
public int getPosY(){
return posY;
}
public void setPosX(int posX){
this.posX=posX;
System.out.println(posX);
}
public void setPosY(int posY){
this.posY=posY;
System.out.println(posY);
}
public Person(String name) {
this.name = name;
}
public Person(String name,int posX,int posY) {
this.name = name;
this.posX= posX;
this.posY= posY;
}
public void placeCharacter(int x, int y){
System.out.println(x+" "+y);
gameMap[b[x]][a[y]] = "⚫";
showMap();
}
}
```package MAP;
导入java.security.publickey;导入java.util.Random;
public class Map {
public String[][] gameMap = {
{ "+---+","+---+","+---+","+---+","+---+","+---+","+---+","+---+","+---+","+---+",},
{ "|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|",},
{ "+---+","+---+","+---+","+---+","+---+","+---+","+---+","+---+","+---+","+---+",},
{ "|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|",},
{ "+---+","+---+","+---+","+---+","+---+","+---+","+---+","+---+","+---+","+---+",},
{ "|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|",},
{ "+---+","+---+","+---+","+---+","+---+","+---+","+---+","+---+","+---+","+---+",},
{ "|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|",},
{ "+---+","+---+","+---+","+---+","+---+","+---+","+---+","+---+","+---+","+---+",},
{ "|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|",},
{ "+---+","+---+","+---+","+---+","+---+","+---+","+---+","+---+","+---+","+---+",},
{ "|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|",},
{ "+---+","+---+","+---+","+---+","+---+","+---+","+---+","+---+","+---+","+---+",},
{ "|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|",},
{ "+---+","+---+","+---+","+---+","+---+","+---+","+---+","+---+","+---+","+---+",},
{ "|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|",},
{ "+---+","+---+","+---+","+---+","+---+","+---+","+---+","+---+","+---+","+---+",},
{ "|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|",},
{ "+---+","+---+","+---+","+---+","+---+","+---+","+---+","+---+","+---+","+---+",},
{ "|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|",},
{ "+---+","+---+","+---+","+---+","+---+","+---+","+---+","+---+","+---+","+---+",},
};
Random rnd = new Random();
public int random1 = rnd.nextInt(9);
public int random2 = rnd.nextInt(9);
public int [] a={1,4,7,10,13,16,19,22,25,28};
public int [] b={1,3,5,7,9,11,13,15,17,19};
public String tree="+";
public void placeObject(){
for(int i = 0; i<10;i++) {
int random3 = rnd.nextInt(5);
if (random3 < 3) {
int random1 = rnd.nextInt(9);
int random2 = rnd.nextInt(9);
gameMap[b[random1]][a[random2]] = tree;
System.out.println(random1 + " " + random2);
}
if(i==9)
showMap();
}
}
public void showMap() {
for (int r=0; r<gameMap.length; r++){
for (int c=0; c<gameMap[r].length; c++){
System.out.print(gameMap[r][c]+" ");
}
System.out.println();
}
}
}
我的一个朋友叫我提取地图,但我不知道该怎么做。这些是对象图像图像
为什么Person扩展映射
?这意味着每个人都是一个映射,而且每个人都有Map
类中每个字段的单独副本,包括Gamemap
。相反,应该将main方法中实例化的映射作为构造函数参数传递给每个人,并将map
作为person
的字段。
public Person(String name, int posX, int posY, Map map) {
this.name = name;
this.posX = posX;
this.posY = posY;
this.map = map;
}
编辑:最后一个问题,如何通过值而不是引用来存储它?
我有两个子类: 行 弧 它们的对象将通过在它们的末端附加它们来创建路径。这些都有自己的“plotCurve”方法。我想将这些对象存储在一个数组中,顺序如下: (我认为“path”应该是超类“path”的对象)当我编写类似这样的代码时; 结果应该显示出来。我应该能看到整条路。(因此,当“直线”的对象出现时,应运行“plotCurve”方法,对于圆弧对象也是如此)。
如何在一个对象中存储对象和数组?更新一个对象内的对象和数组 const obj={key1:1,key2:2,key3:3,keys:['key1','key2','key3']};我在一次采访中得到了这个问题。现在我想在对象中添加一些新数据。2无论何时添加到对象中,都应在该主对象内的数组中更新“key_”(添加了key number)。 输出应该是这样的(你可以使用任何方法/函数/循环来更新它.
我对列有网格。在第二列中,我必须组合框(例如在第一行和第三行)。如何为它们设置不同的存储空间?第二个组合框的值取决于第一个组合框 这是一张图片 我看到一个例子,但是有不同的列,而不是行
问题内容: 我有一个要存储在Redis中的对象数组。我可以分解数组部分,并将它们存储为对象,但是我不知道如何获得类似 然后根据名称搜索数据库,并获取返回的密钥。我需要这样的东西。但几乎无法做到正确。 首先是使这一部分正确。 其次是以某种方式从值中获取密钥,即 我觉得很难。或者,我可以将其直接存储为对象数组并使用简单的for循环。 请建议哪种路线最适合某些实现? 问题答案: 我发现工作是将密钥存储为
Hi必须使用firestore在离线在线synario聊天应用程序。例如,有时用户向其他用户发送消息,但没有internet连接。但当用户打开internet连接时,消息应立即同步到数据基础上。它在firebase中工作,但不知道它在FireStore中是如何工作的。