用户输入java 2d数组

时间:2016-03-16 20:01:57

标签: java arrays 2d

我需要创建代码,使用户输入来创建2d数组的大小(行和列必须小于6),然后用用户给出的值(在-10和10之间)填充它。我获取值的循环根本不起作用,我不知道为什么。这是代码

    import java.util.Scanner;

  public class Matrix {

      public static void main(String[] args) {
          // TODO Auto-generated method stub

    // Implement scanner
    Scanner input = new Scanner(System.in);

    // create loop for accepting matrix input
    // first accept row size
    System.out.println("Please enter the number of rows in your matrix.      Must be 5 or less.");
    int row = input.nextInt();
    while (row > 5 || row < 1) {
        System.out.println("Sorry. Your number is not the correct size. "
                + "Please enter the number of rows in your matrix. Must be between 5 and 1.");
        row = input.nextInt();
    }
    // next accept column size
    System.out.println("Please enter the number of columns in your matrix. Must be 5 or less.");
    int column = input.nextInt();
    while (column > 5 || column < 1) {
        System.out.println("Sorry. Your number is not the correct size. "
                + "Please enter the number of columns in your matrix. Must be between 5 and 1.");
        column = input.nextInt();
    }
    // declare array with row and columns the user gave
    int[][] userArray = new int[row][column];

    // create loop for accepting values within the matrix
    // first loop for row values
    for (int i = 0; i < userArray.length; i++) {
        System.out.println("Please enter numbers between -10 and 10 for your matrix rows");
        int rowValues = input.nextInt();
        while (rowValues > 10 || column < -10) {
            System.out.println(
                    "Sorry. Your number is not the correct size. " + "Please enter a number between 10 and -10.");
            rowValues = input.nextInt();
        }

        // second embedded loop for column values
        for (int j = 0; j < userArray[i].length; j++) {
            System.out.println("Please enter numbers between -10 and 10 for your matrix columns");
            int columnValues = input.nextInt();
            while (columnValues > 10 || column < -10) {
                System.out.println("Sorry. Your number is not the correct size. "
                        + "Please enter a number between 10 and -10.");
                columnValues = input.nextInt();
            }
        }
    }

    printMatrix(userArray);
}

3 个答案:

答案 0 :(得分:0)

这里有几件事。首先,for-loop有两个独立的行和列循环。我假设您的目的是提示用户输入2D阵列的每个单独的单元格,这需要两个嵌套的循环。第二,你的代码示例在任何时候都没有将用户输入分配给变量,所以我假设你的输出只是一堆0值?

你可能正在寻找更像这样的东西:

for (int i = 0; i < userArray.length; i++)
   for(int j = 0; j < userArray[i].length; j++)
   {
      System.out.println("Please enter numbers between -10 and 10 for your matrix");
      int valueIn = input.nextInt();
      while (valueIn > 10 || valueIn < -10)
      {
         System.out.println("Sorry. Your number is not the correct size. " + "Please enter a number between 10 and -10.");
         valueIn = input.nextInt();
      }
      userArray[i][j]=valueIn;
   }

答案 1 :(得分:0)

您需要一个嵌套循环,以读取行和列级别的值。 如下所示:

import java.util.Scanner;

public class Matrix {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        // Implement scanner
        Scanner input = new Scanner(System.in);

        // create loop for accepting matrix input
        // first accept row size
        System.out.println("Please enter the number of rows in your matrix. Must be 5 or less.");
        int row = input.nextInt();
        while (row > 5 || row < 1) {
            System.out.println("Sorry. Your number is not the correct size. "
                    + "Please enter the number of rows in your matrix. Must be between 5 and 1.");
            row = input.nextInt();
        }
        // next accept column size
        System.out.println("Please enter the number of columns in your matrix. Must be 5 or less.");
        int column = input.nextInt();
        while (column > 5 || column < 1) {
            System.out.println("Sorry. Your number is not the correct size. "
                    + "Please enter the number of columns in your matrix. Must be between 5 and 1.");
            column = input.nextInt();
        }
        // declare array with row and columns the user gave
        int[][] userArray = new int[row][column];

        for(int i=0;i< row ; i++){
            for(int j=0; j< column; j++) {
                System.out.print("Please enter the value for array["+i+"]["+j+"] (between -10 and 10):");
                int val = input.nextInt();
                    if(val>10 || val< -10) {
                        System.out.println("Invalid value.");
                        continue;
                    }
                userArray[i][j] = val;
            }
        }
        printMatrix(userArray, row, column);
    }

    public static void printMatrix(int[][] array, int row, int column) {
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < column; j++) {
                System.out.print(array[i][j] + " ");
            }
          System.out.println();
        }
    }
}

答案 2 :(得分:0)

public class Matrix {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        System.out.println("Enter number of rows.");
        int rows = getValueFromConsole(scan, 1, 5);

        System.out.println("Enter number of columns.");
        int cols = getValueFromConsole(scan, 1, 5);

        System.out.println("Enter matrix data.");
        int[][] matrix = createMatrix(scan, rows, cols, -99, 99);

        printMatrix(matrix);
    }

    private static int[][] createMatrix(Scanner scan, int rows, int cols, int min, int max) {
        int[][] matrix = new int[rows][cols];

        for (int row = 0; row < rows; row++)
            for (int col = 0; col < cols; col++)
                matrix[row][col] = getValueFromConsole(scan, min, max);

        return matrix;
    }

    private static int getValueFromConsole(Scanner scan, int min, int max) {
        while (true) {
            System.out.format("Number [%d:%d]: ", min, max);
            int val = scan.nextInt();

            if (val >= min && val <= max)
                return val;

            System.out.println("Sorry. Your number is not correct. Try again.");
        }
    }

    private static void printMatrix(int[][] matrix) {
        for (int row = 0; row < matrix.length; row++) {
            for (int col = 0; col < matrix[row].length; col++)
                System.out.format("%4d", matrix[row][col]);

            System.out.println();
        }
    }
}

输出:

Enter number of rows.
Number [1:5]: 3
Enter number of columns.
Number [1:5]: 3
Enter matrix data.
Number [-99:99]: 10
Number [-99:99]: -11
Number [-99:99]: 12
Number [-99:99]: -13
Number [-99:99]: 14
Number [-99:99]: -15
Number [-99:99]: 16
Number [-99:99]: -17
Number [-99:99]: 18
  10 -11  12
 -13  14 -15
  16 -17  18