调用方法类两次;一次用于行,然后用于列

时间:2015-04-12 02:53:49

标签: java methods bluej matrix-math

为两个输入答案调用方法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;
}

2 个答案:

答案 0 :(得分:1)

您的计划存在冗余。在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: ");

答案 1 :(得分:0)

您正在从主方法

中调用getSize()方法两次
// Calls getSize(args) to get the number for rows and then columns
int rows = getSize(args);
int columns = getSize(args);

在getSize()方法中,您要求输入行数和列数。因此,根据您的代码,它们共同打印4次。

要修复,您可以将其他参数传递给getSize()方法,指定是否采用行或列

e.g。

public static int getSize(boolean isRows){
    // Scanner for keyboard input
    Scanner keyboard = new Scanner(System.in);

    // Declares input as integer
    int input = 0;

    String msg = "Please enter number of ?: ";

    if(isRows)
         msg = msg.replace("?", "rows");
    else
         msg = msg.replace("?", "columns");
    System.out.print(msg);
    input = keyboard.nextInt();

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

这种方法可以进一步重构。