两次调用方法getSize()以获得两个输入答案。
它应该首先行,然后列。
我的问题是它会打印出来。

rows: 6
columns: 4
rows: 6
columns: 4


什么时候应该打印为...

Please enter number of rows: 6

Please enter number of columns: 4


并且是2到6之间的数字。

我知道它运行了4次,因为它两次调用了getSize(args),但我不确定从这里开始。

    public static void main (String [] args)
{
    // Double arrays declared as integers
    int [][] array1;
    int [][] array2;

    // Text to be printed out for user to read
    System.out.print("Welcome to the Matrix Math Calculator\n");
    System.out.print("-------------------------------------\n");
    System.out.print("Each dimension of the matrix must be at least 2,\n");
    System.out.print("and at most 6, in length\n");
    System.out.print("\n");

    // Calls getSize(args) to get the number for rows and then columns
    int rows = getSize(args);
    int columns = getSize(args);

    // Instantiate input for rows and columns into both double arrays
    array1 = new int [rows][columns];
    array2 = new int [rows][columns];

    // For loop to fill in the rows and column values for double array and get random integers
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < columns; j++) {
            array1 [i][j] = (int)(Math.random() * 9 + 1);
            array2 [i][j] = (int)(Math.random() * 9 + 1);
            System.out.printf("%4s",array1[i][j]+ " ");
            System.out.printf("%4s",array2[i][j]+ " ");
        }
        System.out.println(" ");
    }
}

    public static int getSize(String [] args)
{
    // Scanner for keyboard input
    Scanner keyboard = new Scanner(System.in);

    // Declares input as integer
    int input = 0;

    // The string has [2] arguments
    args = new String[2];

    // The string arguments
    args[0] = ("Please enter number of rows: ");
    args[1] = ("Please enter number of columns: ");

    //
    System.out.print(args[0]);
    input = keyboard.nextInt();

    //
    while(input < 2 || input > 7) {
        System.out.print(args[0]);
        input = keyboard.nextInt();
        if (input < 2 || input > 7)
            System.out.println("The number you entered was not between 2 and 6.");
    }


    //
    System.out.print(args[1]);
    input = keyboard.nextInt();

    //
    while(input < 2 || input > 7) {
        System.out.print(args[1]);
        input = keyboard.nextInt();
        if (input < 2 || input > 7)
            System.out.println("The number you entered was not between 2 and 6.");
    }

    //
    return input;
}

最佳答案

您的程序中有冗余。在main()函数中,您两次调用getSize()函数:一次用于行,一次用于列。然后,在函数getSize()中,您将同时获取行和列的输入。因此,最后的输入将发送到您的主要功能。您应该在下面使用此getSize()函数,

          public static int getSize(String args){

      // Scanner for keyboard input
      Scanner keyboard = new Scanner(System.in);

      // Declares input as integer
      int input = 0;

      System.out.print(args);
      input = keyboard.nextInt();

      while(input < 2 || input > 7) {
          System.out.print(args);
          input = keyboard.nextInt();
          if (input < 2 || input > 7)
              System.out.println("The number you entered was not between 2 and 6.");
      }
      return input;
  }


在main()函数中使用此

int rows = getSize("Please enter number of rows: ");
int columns = getSize("Please enter number of columns: ");

10-02 00:47