调用方法 class 两次;一次用于行,然后用于列
Calling method class twice; once for rows, and then for columns
为两个输入答案调用方法 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;
}
您从主方法中调用了两次 getSize()
方法
// Calls getSize(args) to get the number for rows and then columns
int rows = getSize(args);
int columns = getSize(args);
在 getSize() 方法中,您要求输入行数和列数。所以根据你的代码,它总共打印了 4 次。
要解决此问题,您可以将附加参数传递给 getSize() 方法,指定是采用行还是采用列
例如
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;
}
这个方法可以进一步重构很多。
你的程序有冗余。在您的 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: ");
为两个输入答案调用方法 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;
}
您从主方法中调用了两次 getSize()
方法
// Calls getSize(args) to get the number for rows and then columns
int rows = getSize(args);
int columns = getSize(args);
在 getSize() 方法中,您要求输入行数和列数。所以根据你的代码,它总共打印了 4 次。
要解决此问题,您可以将附加参数传递给 getSize() 方法,指定是采用行还是采用列
例如
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;
}
这个方法可以进一步重构很多。
你的程序有冗余。在您的 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: ");