简体   繁体   English

具有用户输入和随机数的2D数组

[英]2D Array with user input and random numbers

I am trying to generate a program that ask the user a number "n" and displays a 2 xn array. 我正在尝试生成一个要求用户输入数字“ n”并显示2 xn数组的程序。 Eg: 例如:

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" “线程“主”中的异常java.lang.ArrayIndexOutOfBoundsException:5”

How can I fix it? 我该如何解决? And I think my Math.random is wrong. 而且我认为我的Math.random是错误的。 Can you guys help me with some advises or where am I doing things wrong? 你们能为我提供一些建议还是我在哪里做错了事? Thanks. 谢谢。

All your errors lie within and after your for-loop: 您的所有错误都在for循环之内和之后:

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] .length,因为数组的第一个维仅存在于0和1之间。A[2]保留2个int图元的空间,第一个在索引0处,最后一个在索引处在索引1处。即使已更改,for循环声明的i变量也将增加到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 =新的int [2] [2];

You can only access A[0][0], A[0][1], A[1][0] & A[1][1]. 您只能访问A [0] [0],A [0] [1],A [1] [0]和A [1] [1]。

you CANNOT access A[2][0], A[2][1] or A[2][2]. 不能访问A [2] [0],A [2] [1]或A [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());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM