首页 > 解决方案 > 如何同时在数组中添加不同的对象并将它们存储起来以供下一步使用?

问题描述

我正在学习 Java 并为我的决赛做游戏项目。我正在尝试向我的数组添加不同的值,但是当我每次在地图上显示不同的对象时打印数组时,我将在此处共享输出。这是主要课程:

import MAP.*;
import Person.*;

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {

        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");
    }
}

这是我的 Person 类:

import MAP.Map;
import java.util.Random;

public class Person extends Map {

    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]] = "⚫&quot;;
        showMap();
    }
}

这是我的地图类:

package MAP;

import Person.Person;

import java.security.PublicKey;
import 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();
        }
    }
} 

我的一位朋友告诉我要提取地图,但我不知道该怎么做。这些是对象 图像 图像

标签: java

解决方案


为什么Person extends Map呢?这意味着每个人都是一个地图,而且每个人都有自己的Map类内每个字段的个人副本,包括gameMap. 相反,您应该将在 main 方法中实例化的地图作为构造函数参数传递给每个人,并且必须mapPerson.

public Person(String name, int posX, int posY, Map map) {
    this.name = name;
    this.posX = posX;
    this.posY = posY;
    this.map = map;
} 

推荐阅读