我正在编写一个程序,它应该创建一个用户定义的数组,然后用随机变量填充它。
我认为我有用于询问用户数组尺寸的功能代码。它必须是介于5和20之间的Int,并且列大小不能与行大小相同。
问题是当我尝试打印零填充阵列时。我无法从Main()调用数组所在的方法。
import java.util.Scanner;
public class Main {
public static void main(String args[]){
Scanner keyboard = new Scanner(System.in);
int row, column;
//Ask user for row size
System.out.println("Enter an integer ROW size from 5 to 20");
row = keyboard.nextInt();
//Checks if input is out of bounds
while(row<5 || row>20){
System.out.println("You did not enter a valid ROW size. Please try again!");
row=keyboard.nextInt();}
//Tells user row size
System.out.println("ROW size: "+row);
//Ask user for column size
System.out.println("\nEnter and integer COLUMN size from 5 to 20. Must be different from ROW size!");
column = keyboard.nextInt();
//Checks if input is out of bounds or repeated
while(column==row || column<5 || column>20){
System.out.println("You did not enter a valid COLUMN size. Please try again!");
column=keyboard.nextInt();}
//Tells user column size
System.out.println("COLUMN size: "+column);
ProcessArray obj = new ProcessArray();
ProcessArray.obj(firstArray);
}
}
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
public class ProcessArray {
/*Main mainObject = new Main();
mainObject.initializeArray(firstArray); */
private int row, column;
private int firstArray[][], secondArray[][];
ProcessArray(int row, int column){
this.row=row;
this.column=column;
//Set array sizes
firstArray = new int[row][column];
secondArray = new int[row][column];
}
//fill first array with zeros
public int[][] initializeArray(int[][] firstArray, int[][] secondArray){
Arrays.fill(firstArray, 0);
Arrays.fill(secondArray, 0);
System.out.println(firstArray);
return firstArray;
}
}
当我在Main()中输入最后几行时,我得到了各种各样的错误。我有什么想法我做错了吗?还有,任何方法来清理/简化我的代码?