2D数组,用户输入和随机数。

时间:2022-08-27 00:23:12

I am trying to generate a program that ask the user a number "n" and displays a 2 x n array. E.g:

我正在尝试生成一个程序,它询问用户一个数字“n”,并显示一个2 x n的数组。例句:

1 2 3 4 5 (User input)

1 2 3 4 5(用户输入)

5 8 2 1 5 (Random numbers)

5 8 2 1 5(随机数)

I can't see to make my code to work. Here is my code:

我不能让我的代码工作。这是我的代码:

import java.util.Scanner;


public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {



        Scanner input = new Scanner(System.in);
        System.out.print("Enter number of exits: ");
        int n = input.nextInt();

        int [][] A = new int[2][n];
        for (int i =0; i <= A[n].length; i++){

            A[i][n]= (int)(Math.random()*10);
    }
        System.out.println(A[2][n]);
        System.out.print("Distance between exit i and exit j is: " + distance());
    }



    public static int distance(){
        Scanner input = new Scanner(System.in);

        System.out.print("Please enter exit i: ");
        int i = input.nextInt();
        System.out.print("Please enter exit j: ");
        int j = input.nextInt();
        return i + j;
    }
}

I am getting this error

我得到这个错误。

"Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5"

“线程中的异常”main“java.lang”。ArrayIndexOutOfBoundsException:5”

How can I fix it? And I think my Math.random is wrong. Can you guys help me with some advises or where am I doing things wrong? Thanks.

我怎样才能修好它?我想我的数学。随机是错误的。你们能帮我一些建议吗?或者我哪里做错了?谢谢。

1 个解决方案

#1


0  

All your errors lie within and after your for-loop:

你所有的错误都在你的循环之内。

for (int i =0; i <= A[n].length; i++){

        A[i][n]= (int)(Math.random()*10);
}

If n = 5, A[5].length does not exist, because the first dimensions of your array only exist between 0 and 1. A[2] reserves space for 2 int primitives, the first is at index 0 and the last is at index 1. Even if that is changed, the i variable your for loop declares is incremented beyond 1 and so the JVM will throw a ArrayIndexOutOfBoundsException.

如果n = 5 A[5]。长度不存在,因为数组的第一个维度只存在于0和1之间。A[2]为2个int原语预留空间,第一个是索引0,最后一个是索引1。即使更改了,我的变量for循环声明的值也会超过1,因此JVM将抛出ArrayIndexOutOfBoundsException。

When declaring an array with the dimensions [2][n], (given n is an Integer, which will be provided by the user via the scanner) you cannot access arrayReference[2][x]

当使用维度[2][n]声明一个数组时,(给定n是一个整数,由用户通过扫描器提供),您不能访问arrayReference[2][x]

Arrays are based on a 0 index structure...

数组基于0索引结构…

Consider the following:

考虑以下:

int [][] A = new int[2][2];

int[] A = new int[2][2];

You can only access A[0][0], A[0][1], A[1][0] & A[1][1].

你只能访问一个[0][0],[0][1],[1][0]和[1][1]。

you CANNOT access A[2][0], A[2][1] or A[2][2].

您不能访问[2][0]、[2][1]或[2][2]。


Here's what you need to do:

以下是你需要做的:

 //A.length will give you the length of the first dimension (2)
 for(int i=0; i<A.length; i++){
            for(int j=0; j<n; j++){
                A[i][j] = (int) (Math.random()*10);
            }
        }
    }
 System.out.println(A[1][n-1]);
 System.out.print("Distance between exit i and exit j is: " + distance());

#1


0  

All your errors lie within and after your for-loop:

你所有的错误都在你的循环之内。

for (int i =0; i <= A[n].length; i++){

        A[i][n]= (int)(Math.random()*10);
}

If n = 5, A[5].length does not exist, because the first dimensions of your array only exist between 0 and 1. A[2] reserves space for 2 int primitives, the first is at index 0 and the last is at index 1. Even if that is changed, the i variable your for loop declares is incremented beyond 1 and so the JVM will throw a ArrayIndexOutOfBoundsException.

如果n = 5 A[5]。长度不存在,因为数组的第一个维度只存在于0和1之间。A[2]为2个int原语预留空间,第一个是索引0,最后一个是索引1。即使更改了,我的变量for循环声明的值也会超过1,因此JVM将抛出ArrayIndexOutOfBoundsException。

When declaring an array with the dimensions [2][n], (given n is an Integer, which will be provided by the user via the scanner) you cannot access arrayReference[2][x]

当使用维度[2][n]声明一个数组时,(给定n是一个整数,由用户通过扫描器提供),您不能访问arrayReference[2][x]

Arrays are based on a 0 index structure...

数组基于0索引结构…

Consider the following:

考虑以下:

int [][] A = new int[2][2];

int[] A = new int[2][2];

You can only access A[0][0], A[0][1], A[1][0] & A[1][1].

你只能访问一个[0][0],[0][1],[1][0]和[1][1]。

you CANNOT access A[2][0], A[2][1] or A[2][2].

您不能访问[2][0]、[2][1]或[2][2]。


Here's what you need to do:

以下是你需要做的:

 //A.length will give you the length of the first dimension (2)
 for(int i=0; i<A.length; i++){
            for(int j=0; j<n; j++){
                A[i][j] = (int) (Math.random()*10);
            }
        }
    }
 System.out.println(A[1][n-1]);
 System.out.print("Distance between exit i and exit j is: " + distance());