0

我对如何开始我正在做的这个问题有点困惑。我试图有一个二维数组,最终将计算两点之间的距离。我对如何设置阵列感到困惑。它的工作方式是用户会给我点的数量(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 坐标,然后用它们填充数组。我该怎么做呢?

4

1 回答 1

0

既然你已经知道你只有 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;
    }
于 2012-10-20T23:33:44.277 回答