我制作了一个Magic Square程序并且它已经完成了,但是,我的类中的一个方法无法从main方法调用。我在课堂上有两种方法,但是找不到两种方法中的一种。 noRep
是一种确保输入的数字不重复的方法。当我尝试从main方法中使用它时,编译器会说
找不到符号方法noRep(int [] [])
这是班级:
public class MagicClass
{
public static boolean noRep(int[][] square)
{
int[] one = new int[10];
for (int i = 1; i < 10; i++)
one[i] = 0;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
if (square[i][j] < 1 || square[i][j] > 9)
return false;
one[square[i][j]]++;
}
}
for (int i = 1; i < 10; i++)
if (one[i] != 1)
return false;
return true;
}
public static boolean checkSums(int[][] square)
{
for (int i = 0; i < 3; i++)
{
int sum = 0;
for (int j = 0; j < 3; j++)
sum += square[i][j];
if (sum != 15)
return false;
}
for (int j = 0; j < 3; j++)
{
int sum = 0;
for (int i = 0; i < 3; i++)
sum += square[i][j];
if (sum != 15)
return false;
}
if (square[0][0] + square[1][1] + square[2][2] != 15)
return false;
if (square[0][2] + square[1][1] + square[2][0] != 15)
return false;
return true;
}
}
这是主要方法:
import java.util.*;
import java.util.Scanner;
public class MagicSquares {
public static void main(String[] args) {
int[][] square = new int[3][3];
Scanner input = new Scanner(System.in);
MagicClass MagicSqr = new MagicClass();
//checkFrequency Frequent = new checkFrequency(square); TESTING
//void Fre = MagicClass.checkFrequency (square); TESTING
System.out.println("Please enter your magic square.");
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
square[i][j] = input.nextInt();
if (checkSums(square && noRep(square)))
System.out.println("You have a magic square");
else
System.out.println("Not a magic square");
}
}
答案 0 :(得分:3)
您在另一个班级中调用noRep。 由于它是静态方法(类级方法),因此不需要实例。使用以下方法调用它:
if (MagicClass.checkSums(square) && MagicClass.noRep(square))