import java.util.*;
public class ManyMethods
{
static int row, col, n, rows, columns, listSize;
static Scanner console = new Scanner (System.in);
public static void createArithmeticSeq(int [] list)
{
int first;
int diff;
//prompt user for first and diff to create list
System.out.println("Choose the first number and diff to create" +
" an arithmetic sequence.");
System.out.println("Enter first and diff : ");
first = console.nextInt();
diff = console.nextInt();
//process to create list of n*n elements
for (int i=0; i<listSize; i++)
{
list[i]=first+i*diff;
}
}
public static void matricize (int [] list, int [][] matrix)
{
System.out.println("Matricize arithmetic sequence:");
int i = 0;
//loop through each row
for (row=0; row<matrix.length; row++)
{
//loop through each column
for (col=0; col<matrix[row].length; col++)
{
//populate matrix with values from list
matrix[row][col] = list[i++];
}
}
}
public static void printMatrix(int [][] matrix)
{
for (row=0; row < matrix.length; row++)
{
for (col=0; col < matrix[row].length; col++)
System.out.printf("%2d" + " ", matrix[row][col]);
System.out.println("\n");
}
}
public static void reverseDiagonal(int [] [] matrix)
{
System.out.println("Reverse the diagonals:");
int temp;
for (row=0; row<matrix.length / 2; row++)
{
temp = matrix[row][row];
matrix[row][row] =
matrix[matrix.length - 1 - row] [matrix.length - 1 - row];
matrix[matrix.length - 1 - row][matrix.length - 1 - row] = temp;
}
for (row=0; row<matrix.length / 2; row++)
{
temp = matrix[row][matrix.length - 1 - row];
matrix[row][matrix.length - 1 - row] =
matrix[matrix.length - 1 - row][row];
matrix[matrix.length - 1 - row][row] = temp;
}
}
public static void magicCheck(int [] list, int [] [] matrix)
{
System.out.println("Is the matrix a magic square?");
int sum=0, sumRow=0, sumCol=0, sumDiag1=0, sumDiag2=0, magicNumber=0;
for(int i=0; i<listSize; i++)
{
sum = sum + list[i];
magicNumber = (sum/n);
for(row=0; row<matrix.length; row++)
{
//sum each row, then compare to magicNumber
for(col=0; col<matrix[row].length; col++)
sumRow = sumRow + matrix[row][col];
while (sumRow == magicNumber)
{
for(col=0; col<matrix.length; col++)
{
for(row=0; row<matrix[col].length; row++)
{
sumCol = sumCol + matrix[row][col];
while (sumCol == magicNumber)
{
for (int row=0; row<matrix.length; row++)
{
sumDiag1 += matrix[row][row];
}
while (sumDiag1 == magicNumber)
{
for (int row=n-1; row>=0; row--)
sumDiag2 += matrix[row][row];
while(sumDiag2 == magicNumber)
System.out.println("It is a magic square.");
System.out.println("The magic number is " + magicNumber + ".");
}
}
}
}
}
}
}
System.out.println("It is not a magic square.");
}
public static void main (String [] args)
{
int [] list = new int [16];
createArithmeticSeq (list);
int [] [] matrix = new int [rows] [columns];
matricize(list, matrix);
printMatrix(matrix);
System.out.print("\n");
reverseDiagonal(matrix);
printMatrix(matrix);
magicCheck(list, matrix);
}
}
我一直在尝试这个并且我不知道我搞砸了什么都没有出现,我不知道如何解决这个问题。这些是我必须创建这个程序的问题。
1.编写一个名为createArithmeticSeq的方法,提示用户输入2个数字,first和diff。然后,该方法创建一个名为A的数组,该数组在算术序列中排序为16个元素。我们被告知使用21和5所以顺序应该是: 21 26 31 36 41 46 51 56 61 66 71 76 81 86 91 96
2.编写一个名为matricize的方法,该方法采用一个包含16个元素的1-d数组和一个包含4行和4列的2 d数组作为参数。此方法将1-d数组的元素放入2-d数组中。使用1-d数组A和B是2-d数组,然后将A的元素放入B后,数组B为: 21 26 31 36 41 46 51 56 61 66 71 76 81 86 91 96
3.编写一个名为reverseDiagonal的方法,它反转二维数组的两个对角线。反转2-d数组后使用2-d数组B. 96 26 31 81 41 71 66 56 61 51 46 76 36 86 91 21
5.write方法printMatrix输出二维数组的元素。每行一行。
答案 0 :(得分:0)
在这一行上你声明了一堆静态变量:
static int row, col, n, rows, columns, listSize;
但是您从未向n
,rows
,columns
或listSize
分配任何内容,因此它们的默认值为0,这会导致各种问题:
list
数组,但在循环时不使用list.length
;您使用单独的变量listSize
,即0。rows
和columns
变量(0),这样就可以获得大小为0×0的矩阵。n
为0,sum/n
将抛出ArithmeticException
(由于除以0),但该行未运行,因为它位于{{1}内部} loops。同时,那里不应存在listSize
和row
个变量。它们使用它们作为索引变量的各种col
循环不那么健壮,因为它们共享变量而你不能,例如,嵌套对这些方法的调用。您应该完全删除这两个静态变量,并在for
循环中声明它们。
您也可以删除该行上的其余变量,并根据需要在较窄的范围内声明它们和/或使用数组for
字段。