I’m a bit confused on how to start this problem I’m doing. I am trying to have a 2-d array that eventually will compute the distances between two points. I’m confused about how to go about setting up the array. The way it works is that the user will give me the amount of points (x, y coordinates). That will be the length of both arrays (row and column). I now need to fill in the array with the user’s input. Here’s how I have it set up so far:
int points, x , y;
Scanner scan= new Scanner(System.in);
System.out.println("Please enter the number of points: ");
points = scan.nextInt();
int[][] coord = new int[points][points];
for(int i =0; i < coord.length; i++)
for(int j = 0; j < coord[i].length; j++){
System.out.println("Please enter the x coordinates: ");
x = scan.nextInt();
System.out.println("Please enter the y coordinates: ");
y = scan.nextInt();
}
}
I’m trying to get the x and y coordinates separately and then fill in the array with them. How would I go about doing that?
Since you already know that you are having only x and y cooridnates, I am hardcoding this number.
Change your code like this :