繁体   English   中英

驱动程序为什么无法在类中找到方法?

[英]Why can't the driver find a method in a class?

我编写了一个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");
}
}

您在另一个类中调用noRep。 由于它是静态方法(类级别方法),因此不需要实例。 使用以下命令调用它:

if (MagicClass.checkSums(square) && MagicClass.noRep(square))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM