简体   繁体   English

用户输入 java 2d 数组

[英]User Input java 2d Array

I need to create code that takes user input to create the size of the 2d array (rows and columns must be less than 6), then fill it up with values that the user gives (between -10 and 10).我需要创建接受用户输入的代码来创建二维数组的大小(行和列必须小于 6),然后用用户提供的值(-10 到 10 之间)填充它。 My loop for taking the values isn't working at all and I'm not sure why.我的取值循环根本不起作用,我不知道为什么。 Here's the code这是代码

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

Couple of things here.这里有几件事。 First, your for-loop has two separate loops for the rows and columns.首先,您的 for 循环有两个单独的行和列循环。 I assume what you're going for is prompting the user for each individual cell of the 2D array, which would require two nested loops.我假设您要做的是提示用户输入 2D 数组的每个单独的单元格,这需要两个嵌套循环。 Second, at no point does your code example assign the user input to a variable, so I presume your output is just a bunch of 0 values?其次,您的代码示例绝不会将用户输入分配给变量,所以我认为您的输出只是一堆 0 值?

You're probably looking for something more like this:您可能正在寻找更像这样的东西:

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

You need a nested loop, to read values at row and then column level.您需要一个嵌套循环来读取行级别和列级别的值。 Something like below:如下所示:

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

Output:输出:

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

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

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