簡體   English   中英

如何創建一個數組,用於存儲來自連續用戶輸入的x,y坐標?

[英]How do I make an array for storing x, y co-ordinates from continuous user input?

我目前正在用Java制作RobotArena游戲,其中一項任務是制作一個數組,用於存儲游戲中的每個機器人。 例如,如果用戶在游戲中需要2個機器人...我將必須將2個機器人存儲在一個數組中(它們的x和y坐標),或者如果用戶想要的機器人不只是2個。

我為機器人坐標制作了一個2D數組,但它僅將機器人的最新X / Y坐標存儲在數組中,而不存儲以前的機器人坐標。

有沒有辦法做到這一點? 將其分配給變量還是置於循環中? 任何答案將不勝感激。

這是我到目前為止所擁有的代碼,我是Java的新手,所以如果代碼不太好,很抱歉。

public static class RobotArena{
    public RobotArena(int x, int y){
        int Arena[][] = new int[4][4]; //Sets the 2d array
        Arena[x][y] = '1'; //Puts 1 where the X/Y co ordinate is

        for (int[] a : Arena) { //this prints out the grid
            for (int i : a) {
                System.out.print(i + " ");
            }
            System.out.println("\n");
        }

        public static void main(String[] args) {

            Scanner s = new Scanner(System.in);
            System.out.println("How many Robots in your world?");
            int numRobots = s.nextInt();

            Robot[] allRobots = new Robot[numRobots];
            Robot.RobotArena[] allRobots2 = new Robot.RobotArena[numRobots];

            int rx, ry;

            System.out.println("Now enter position of each robot in turn (as x y) >");
            for (int ct = 0; ct<numRobots; ct++) {
                System.out.println("Enter, x y position for " + ct + "th robot: >");
                rx = s.nextInt();
                ry = s.nextInt();
                allRobots[ct] = new Robot(rx, ry);
                allRobots2[ct] = new RobotArena(rx, ry);
                allRobots[ct].printIdPosition(); 
            }       
        }
    }
}

這是我在游戲中添加2個機器人時的輸出:

機械手1(1,1)的坐標;

0 0 0 0 
0 49 0 0 
0 0 0 0 
0 0 0 0 

當我將第二個機器人添加到游戲中時,這是輸出,坐標為(2,2):

0 0 0 0 
0 0 0 0 
0 0 49 0 
0 0 0 0 

我也希望第一個機器人也顯示最新的成績,但是我不確定該如何進行。

我寧願這樣:

•僅使用一個變量來初始化所有機械手

•使用私人成員初始化Arena

•制定一種可以設置坐標的方法

•制作可以打印競技場的方法。

public static class RobotArena{
    int Arena[][] = new int[4][4];


public void setCoordinates(int x,int y){
    Arena[x][y] = '1';
}
public void printArena(){
     for (int[] a : Arena) { //this prints out the grid
        for (int i : a) {
            System.out.print(i + " ");
        }
        System.out.println("\n");
    }
}
}

    public static void main(String[] args) {

        Scanner s = new Scanner(System.in);
        System.out.println("How many Robots in your world?");
        int numRobots = s.nextInt();

       RobotArena allRobots=new RobotArena();

        int rx, ry;

        System.out.println("Now enter position of each robot in turn (as x y) >");
        for (int ct = 0; ct<numRobots; ct++) {
            System.out.println("Enter, x y position for " + ct + "th robot: >");
            rx = s.nextInt();
            ry = s.nextInt();
            allRobots.setCoordinates(rx, ry);

            //allRobots[ct].printIdPosition(); 
        }
        allRobots.printArena();
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM