【问题标题】:User Input java 2d Array用户输入 java 2d 数组
【发布时间】:2016-03-16 20:01:57
【问题描述】:

我需要创建接受用户输入的代码来创建二维数组的大小(行和列必须小于 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);
}

【问题讨论】:

  • 欢迎来到 SO。请更具体地了解“我的取值循环根本不起作用,我不确定为什么。”。为什么它不工作,它应该如何工作?此外,考虑创建一个Minimal, Complete, and Verifiable example

标签: java arrays 2d


【解决方案1】:

这里有几件事。首先,您的 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;
   }

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:
      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
      

      【讨论】:

        猜你喜欢
        • 2023-03-23
        • 2021-12-07
        • 1970-01-01
        • 2020-06-07
        • 2016-01-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-09-27
        相关资源
        最近更新 更多