[英]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.