我对如何开始我正在做的这个问题感到有点困惑。我正在尝试一个二维数组,它最终将计算两个点之间的距离。我对如何设置数组感到困惑。它的工作方式是用户会给我点的数量(x,y坐标)。这将是两个数组(行和列)的长度。现在我需要用用户的输入填充数组。到目前为止,我的设置如下:
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();
}
}我尝试分别获得x和y坐标,然后用它们填充数组。我该怎么做呢?
发布于 2012-10-21 07:33:44
因为你已经知道你只有x和y坐标,所以我硬编码这个数字。像这样修改你的代码:
int[][] coord = new int[points][2];
for(int i =0; i < points; i++) {
System.out.println("Please enter the x coordinates: ");
x = scan.nextInt();
System.out.println("Please enter the y coordinates: ");
y = scan.nextInt();
coord[i][0]=x;
coord[i][1]=y;
}https://stackoverflow.com/questions/12993560
复制相似问题