简体   繁体   English

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

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

I made a Magic Square program and it is about done, however, One of the methods in my class can't be called from the main method. 我编写了一个Magic Square程序,并且即将完成,但是,不能从main方法中调用类中的方法之一。 I have two methods in the class and only one out of the two is not found. 我在该类中有两种方法,但找不到两种方法中的一种。 noRep is a method that makes sure the inputted numbers aren't repeated. noRep是确保输入的数字不重复的方法。 When I try to use it from the main method, the compiler says 当我尝试从main方法使用它时,编译器会说

cannot find symbol method noRep (int[][]) 找不到符号方法noRep(int [] [])

Here is the class: 这是课程:

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;
}
}

Here is the main method: 这是主要方法:

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");
}
}

You're calling noRep in a different class. 您在另一个类中调用noRep。 Since it is a static method (class level method), instance is not needed. 由于它是静态方法(类级别方法),因此不需要实例。 Call it using the following: 使用以下命令调用它:

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

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

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