繁体   English   中英

带有用户输入的 Java 2D 数组

[英]Java 2D array with user input

当我输入数字时,我遇到了我的 java 程序的问题,它会返回索引越界错误。 这条线是 66,它被赶上了。

    arrayName[row][col] = holder;

找出问题的任何帮助将是最有帮助的。 完整程序如下

   package workfiles;

   import java.util.*;
   import java.util.Scanner;

 public class prob2 {

// Do not modify this method
public static void main(String[] args) {

    try
    {
        int [][] iArray = enter2DPosArray();
        System.out.println("The original array values:");
        print2DIArray(iArray);
        int [][] tArray = transposition(iArray);
        System.out.println("The transposed array values:");
        print2DIArray(tArray);
    }

    catch (InputMismatchException exception)
    {
        System.out.println("The array entry failed. The program will now halt.");
    }

}

    // A function that prints a 2D integer array to standard output
    // It prints each row on one line with newlines between rows
    public static void print2DIArray(int[][] output) {

    }

// A function that enters a 2D integer array from the user
// It raises an InputMismatchException if the user enters anything other
// than positive (> 0) values for the number of rows, the number of
// columns, or any array entry
public static int[][] enter2DPosArray() throws InputMismatchException {

    int row=0;
    int col=0;
    int arow=0;
    int acol=0;
    int holder=0;
    Scanner numScan = new Scanner(System.in);

    while (row<=0){
        System.out.print("How many rows (>0) should the array have? ");
        row = numScan.nextInt();
    }

    while (col<=0){
        System.out.print("How many columns (>0) should the array have? ");
        col = numScan.nextInt();
    }
    int[][] arrayName = new int[row+1][col+1];

    while (arow < row) {

        if (acol<=col)
            System.out.println("Enter a positive (> 0) integer value: ");
            holder = numScan.nextInt();
   // !!!line 66 begins right here!!!
            arrayName[arow][acol] =  holder;
            acol ++;

        if (acol>col)
            acol=0;
            arow ++;
            System.out.println("Enter a positive (> 0) integer value: ");
            holder = numScan.nextInt();
            arrayName[arow][acol] = holder;
            acol ++;



    }
    //arrayName[i][j]
    numScan.close();
    return arrayName;
}

public static int[][] transposition(int [][] arrayName) {

    int r=0, c=0;

    int[][] transpose = new int[r][c];
    for (int i = 0; i < r; i++) {
        for (int j = 0; j < c; j++) {
            transpose[i][j] = arrayName[j][i];
        }
    }
    return transpose;
}

 }

我将 eclipse 重新安装到我的计算机并删除了一个 while 循环,该循环将值一直设置为零,并将其更改为 if 语句,因此它只发生一次,我无法解释它是如何或为什么会出现越界错误,但是该程序现在可以正常工作。

    package workfiles;



    import java.util.*;
    import java.util.Scanner;

    public class hw2 {

   // Do not modify this method
   public static void main(String[] args) {

    try
    {
        int [][] iArray = enter2DPosArray();
        System.out.println("The original array values:");
        print2DIArray(iArray);
        int [][] tArray = transposition(iArray);
        System.out.println("The transposed array values:");
        print2DIArray(tArray);
    }

    catch (InputMismatchException exception)
    {
        System.out.println("The array entry failed. The program will now halt.");
    }

}

    // A function that prints a 2D integer array to standard output
    // It prints each row on one line with newlines between rows
    public static void print2DIArray(int[][] output) {
    int iArray[][];

        for (int row = 0; row < iArray.length; row++) {
            for (int column = 0; column < iArray[row].length; column++) {
                System.out.print(iArray[row][column] + " ");
            }
            System.out.println();
        }
    }



// A function that enters a 2D integer array from the user
// It raises an InputMismatchException if the user enters anything other
// than positive (> 0) values for the number of rows, the number of
// columns, or any array entry
public static int[][] enter2DPosArray() throws InputMismatchException {

    int row=0;
    int col=0;
    int arow=0;
    int acol=0;
    int holder;
    Scanner numScan = new Scanner(System.in);

    while (row<=0){
        System.out.print("How many rows (>0) should the array have? ");
        row = numScan.nextInt();
    }

    while (col<=0){
        System.out.print("How many columns (>0) should the array have? ");
        col = numScan.nextInt();
    }
    int[][] iArray = new int[row][col];

    while (arow < row) {

        while (acol < col) {
            System.out.println("Enter a positive (> 0) integer value: ");
            holder = numScan.nextInt();
            iArray[arow][acol] = holder;
            acol++;
        }

     //this is where i replaced the while loop for the if statment, in the while loop it kept on resetting the acol value. there was no need to loop that part of the program.    

     if  (acol >= col) {
           acol = 0;
           arow ++;

        }



    }
    //arrayName[i][j]
    numScan.close();
    return iArray;
}

public static int[][] transposition(int [][] iArray) {

    int r=0, c=0;

    int[][] transpose = new int[r][c];
    for (int i = 0; i < r; i++) {
        for (int j = 0; j < c; j++) {
            transpose[i][j] = iArray[j][i];
        }
    }
    return transpose;
  }

}

试试这个:

    if (acol <= col) {
        System.out.println("Enter a positive (> 0) integer value: ");
        holder = numScan.nextInt();
        arrayName[arow][acol] = holder;
        acol++;
    }
    if (acol > col) {
        acol=0;
        arow ++;
        System.out.println("Enter a positive (> 0) integer value: ");
        holder = numScan.nextInt();
        arrayName[arow][acol] = holder;
        acol++;
    }

暂无
暂无

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

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