Java类没有main方法

标签 java class-method

我需要运行 GCD 的方法代码。我的 java 文件名为“GCD.java”,公共(public)类名为“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;
          }
    }

最佳答案

对于没有 main 方法的类,或者它有一个未声明为 public static void main(String[] args) 的 main 方法,这是完全有效的.

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

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

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

编译后你可以运行:

java GcdLauncher

或者您可以向 GCD 类添加一个 public static void main(String[] args) 方法。

我强烈建议您更改您的 getDivisor 方法,使其不包含参数 - 无论如何,您实际上并没有使用它们......

关于Java类没有main方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22440497/

相关文章:

ruby-on-rails - 当邮件程序未被定义时,您如何在邮件程序上调用类方法?

objective-c - 使用某个对象类的类方法

python - 树数据结构中的根节点操作

java - 如何将字符串打印到另一个应用程序的对话框?

java - 什么时候使用 "getResourceAsStream"方法?

java - 使用相同的文字/字符串对象作为键时 HashMap 的大小?

python - 装饰类继承和类方法

c++ - 将参数 move 到数据成员 : Take parameter by copy or rvalue-ref?

java - 如何在 Spring Cloud Gateway 后面连接 React UI? (不使用 Zuul)

java - 有和没有任何声明的构造函数