用户输入java 2d数组

问题描述 投票:0回答:3

我需要创建接受用户输入的代码来创建二维数组的大小(行和列必须小于 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);
}
java arrays 2d
3个回答
0
投票

这里有几件事。首先,您的 for 循环有两个单独的行循环和列循环。我假设您想要提示用户输入 2D 数组的每个单独单元格,这将需要两个 nested 循环。其次,您的代码示例在任何时候都不会将用户输入分配给变量,所以我认为您的输出只是一堆 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;
   }

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();
        }
    }
}

-1
投票
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
© www.soinside.com 2019 - 2024. All rights reserved.