简体   繁体   English

如何创建一个数组,用于存储来自连续用户输入的x,y坐标?

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

I'm currently making a RobotArena game on Java and one of the tasks is to make an array for storing each robot in the game. 我目前正在用Java制作RobotArena游戏,其中一项任务是制作一个数组,用于存储游戏中的每个机器人。 For example, if the user wants 2 robots in the game... I'll have to store the 2 robots in an array (the x and y co ordinates of them) or if the user wants more Robots than just 2. 例如,如果用户在游戏中需要2个机器人...我将必须将2个机器人存储在一个数组中(它们的x和y坐标),或者如果用户想要的机器人不只是2个。

I've made a 2D array for the robot co ordinates but it only stores the latest X/Y co-ordinate of the robot in the array and doesn't store the previous robots co ordinate. 我为机器人坐标制作了一个2D数组,但它仅将机器人的最新X / Y坐标存储在数组中,而不存储以前的机器人坐标。

Is there a way to do this? 有没有办法做到这一点? Assigning it to a variable or putting it in a loop? 将其分配给变量还是置于循环中? Any answers will be appreciated. 任何答案将不胜感激。

This is the code I have so far, I'm new to Java, so I'm sorry if the code isn't too good. 这是我到目前为止所拥有的代码,我是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(); 
            }       
        }
    }
}

This is the output when I add 2 robots to the game: 这是我在游戏中添加2个机器人时的输出:

Co-ordinate of Robot 1 (1, 1); 机械手1(1,1)的坐标;

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

When I add the 2nd robot to the game, this is the output, with co-ord (2, 2): 当我将第二个机器人添加到游戏中时,这是输出,坐标为(2,2):

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

I want the 1st robot to be displayed in the latest grade too, but I'm not sure how to go round doing so. 我也希望第一个机器人也显示最新的成绩,但是我不确定该如何进行。

I would rather do it this way: 我宁愿这样:

• Use only one variable to initialize all robots •仅使用一个变量来初始化所有机械手

• Use a private member to initialize Arena •使用私人成员初始化Arena

• Make a method that would set Co-ordinates •制定一种可以设置坐标的方法

• Make a method that would print the 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.

相关问题 LibGDX:在LibGDX中旋转形状XY坐标后,如何更改它的形状? - LibGDX: How do I change a shapes X Y co-ordinates after rotating it in LibGDX? 屏幕X,Y坐标已转换为地图Lon / Lat坐标 - Screen X,Y co-ordinates translated to map Lon/Lat co-ordinates 在快速投掷时,慢速投射的XY共同输入更多但更少 - X-Y Co-Ordinates input on slow fling are more but less on fast fling 是否需要从图像图表中提取x和y轴的两个坐标值? - Need two extract co-ordinates value of x & y axis from a image chart? 如何使用 Selenium-Webdriver 仅使用 x,y 坐标来使用 mouseMove() 函数? - How to use mouseMove() function using only x,y co-ordinates using Selenium-Webdriver? 如何在Netbeans的JFrameForm中将组件的当前X和Y坐标(如按钮)记录到变量中 - How to record current X and Y co-ordinates of a Component (like a Button) into a Variable in a JFrameForm in Netbeans 如何从坐标列表中计算到给定点的最近坐标 - How to calculate the closest co-ordinates to a given point from a list of co-ordinates 如何使用二维数组存储坐标? - How to store co-ordinates using a two dimensional array? 如何仅从Servlet请求新的或更改的坐标? - how to request only new or changed co-ordinates from a servlet? 如何从Chrome浏览器获取画布元素坐标 - How to get canvas element co-ordinates from chrome browser
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM