Моя цель состоит в том, чтобы создать три разных 2D-массива и вместо двух последних заменить все нули в значениях массива вместо пробелов.Я попытался использовать printf ("% s", "[]"), но вместо замены нулей они просто добавляют скобки над массивом.я верю, что это как-то связано с размещением моего оператора else.
public class arrayprgm1{
public static int[][] Table(int x, int y){//creating first 2D array
int[][] array = new int[x][y];
for (int row= 0; row < array.length; row++){// using for loop to create array
for (int column=0;column<array[row].length;column++){//nested for loop
array[row][column]=(row+column)*3;// algorithm used for first 2D array
}
}
int sum = 0;//declaring integer to sum up all of the values created by array
for (int row=0; row < array.length; row++){
for (int column=0; column < array[row].length; column++){
sum = sum + array[row][column];// algorithm used to sum up values of first array
}
}
System.out.printf("The total of all the numbers added together is: %2d\n", sum);// using format print to display sum
return array;//return array created to the main method
}//end of Table method
public static int[][] Odds(int x, int y){//creating second 2D array
int[][]array = new int[x][y];
for (int row= 0; row < array.length; row++){// using for loop to create array
for (int column=0;column<array[row].length;column++){//nested for loop
{
if ((row + column)%2 !=0){ // if statement used to determine if value is odd or even
array[row][column]=(row+column)*3;// algorithm used for second array
}
else
System.out.printf("%s","[]");
}
}
}
int sum = 0;//declaring integer to sum up all of the values created by array
for (int row=0; row < array.length; row++){
for (int column=0; column < array[row].length; column++){
sum = sum + array[row][column];// algorithm used to sum up all odd values
}
}
for(int row=0; row< array.length;row++){
for(int column=0;column<array[row].length;column++){
}
}System.out.printf("The total of all the odd numbers added together is: %2d\n", sum);//format print used to display sum of all odd numbers
return array;
}//end of Odds Method
public static int[][] Evens(int x, int y){ //Creating the third 2D array that will hold all even values
int[][] array = new int[x][y];
for (int row= 0; row < array.length; row++){//for loop to create array
for (int column=0;column<array[row].length;column++){//nested for loop
{
if ((row + column)%2 ==0){//if statement used to determine if value is odd or even
array[row][column]=(row+column)*3;//algorithm used for third array
}
else
System.out.printf("%s","[]");
}
}
}
int sum = 0;// declaring integer to sum up all the even values
for (int row=0; row < array.length; row++){//for loop to create array
for (int column=0; column < array[row].length; column++){//nested for loop
sum = sum + array[row][column];// algorithm used to sum up all even numbers
}
}
System.out.printf("The total of all the even numbers added together is: %2d\n", sum);// format print used to display sum of all even numbers
return array;
}//end of Evens method
public static void main(String[] args){//main method
int[][]array = new int[15][15];//create array to pass to arrays method
array = Table(15,15);
for(int row=0; row < array.length; row++){ //for loop to properly display array in a square
for(int column=0; column<array[row].length;column++){//nested for loop
System.out.printf("%4d",array[row][column]);//format the print
}
System.out.printf("\n");
}
System.out.printf("\n");
array = Odds(15,15);
for(int row=0; row < array.length; row++){//for loop to properly display array in square
for(int column=0; column<array[row].length;column++){//nested for loop
System.out.printf("%4d",array[row][column]);//format the print
}
System.out.printf("\n");
}
System.out.printf("\n");
array = Evens(15,15);
for(int row=0; row < array.length; row++){//for loop to properly display array in square
for(int column=0; column<array[row].length;column++){//nested for loop
System.out.printf("%4d",array[row][column]);//format the print
}
System.out.printf("\n");
}
}//end of main method
}//end of public class arrayprgm1