Java 类没有 main 方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22440497/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 15:44:00  来源:igfitidea点击:

Java class doesn't have main method

javaclass-method

提问by user3047713

I need to run a method code for GCD. My java file is called "GCD.java" and the public class is called "GCD." Yet I keep getting the message "Class GCD does not have a main method" even though I have no red explanation point circles in any of my lines. I can run the code without the method code (i.e. public static void main(String[] args)), but I need to run the code with a method. Thanks.

我需要为 GCD 运行一个方法代码。我的 java 文件称为“GCD.java”,公共类称为“GCD”。然而,即使我的任何一行中都没有红色的解释点圆圈,我仍然收到消息“GCD 类没有主要方法”。我可以在没有方法代码的情况下运行代码(即 public static void main(String[] args)),但我需要使用方法运行代码。谢谢。

==========================

==========================

import java.util.Scanner;

    public class GCD
    {

        public static int getDivisor(int x, int y)
        {


        System.out.println("Greatest Common Divisor Finder");
        System.out.println();

        String choice = "y";
        Scanner sc = new Scanner(System.in);
        while (choice.equalsIgnoreCase("y"))
        {

            System.out.print("Enter first number: ");
            x = sc.nextInt();
            System.out.print("Enter second number: ");
            y = sc.nextInt();

            int secondNumber = 0;
            int firstNumber = 0;
            int Greatestcommondivisionfinder = 0;

            // x = first,  y = second
            if (x > y)
                {
                    do
        {
                        x -= y;
                        }
             while (x > y);
        do
        {
                        y -= x;
                        }
    while (y > 0);
            System.out.println("Greatest Common Divisor: " + x);
        }

            else if (y > x)
        {
        do
                        {
            y -= x;
                        }
    while(y > x);
        do
                        {
            x -= y;
                        }
    while (x > 0);
            System.out.println("Greatest Common Divisor: " + y);
        }
             else
                    {
                    int subtract;
                    do
                    {
                    subtract = (int)y - (int)x;
                    }

            while(y > x);
            int gcd;
            gcd = (int)x - subtract;
                    }


            System.out.println();
            System.out.print("Continue? (y/n): ");
            choice = sc.next();
            System.out.println();
                }
            return 0;
          }
    }

回答by Jon Skeet

It's entirely valid for a class not to have a mainmethod - or for it to have a main method which isn't declared as public static void main(String[] args).

对于一个没有main方法的类或者它有一个没有声明为public static void main(String[] args).

However, in order to treat a class as the entry pointfor a Java application, it needs that method, with that signature (although the parameter name can vary).

但是,为了将类视为Java 应用程序的入口点,它需要具有该签名的方法(尽管参数名称可能会有所不同)。

So basically, you've got a class which is fine in itself, but you can't launch on its own. You could create a separate class, e.g.

所以基本上,你有一个本身很好的类,但你不能自己启动。您可以创建一个单独的类,例如

public class GcdLauncher {
    public static void main(String[] args) {
        GCD.getDivisor(0, 0); // Parameters are ignored anyway...
    }
}

Then after compilation you could run:

然后编译后你可以运行:

java GcdLauncher

Or you could add a public static void main(String[] args)method to your GCDclass.

或者你可以public static void main(String[] args)在你的GCD类中添加一个方法。

I would strongly advise you to change your getDivisormethod not to have parameters though - you're not actually using them anyway...

我强烈建议你改变你的getDivisor方法不要有参数 - 你实际上并没有使用它们......

回答by Pablo Francisco Pérez Hidalgo

If your class is to be used as a main program, it has to implement an

如果你的类要用作主程序,它必须实现一个

public static void main(String[] args))

Method from where you can call your GCD method.

可以从中调用 GCD 方法的方法。

回答by Pradeep Simha

Yes, as your Eclipse correctly says, you don't have mainmethod in your GCD.java file. In-order run this class independently, you need to have main method. Otherwise you can only create Object of this class and call from other class.

是的,正如您的 Eclipse 正确说的那样,您main的 GCD.java 文件中没有方法。为了独立运行这个类,你需要有 main 方法。否则你只能创建这个类的对象并从其他类调用。