Java 错误:在Calculate 类中找不到Main 方法,请将main 方法定义为:public static void main(String[] args)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20268262/
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 00:43:37  来源:igfitidea点击:

Error: Main method not found in class Calculate, please define the main method as: public static void main(String[] args)

javamethodsmain

提问by user3046225

I need help with the main method, I'm getting this error:

我需要 main 方法的帮助,我收到此错误:

Error: Main method not found in class Calculate, please define the main method as:
    public static void main(String[] args)

Here's the code:

这是代码:

class Calculate {

private double fn;
private double sn;
private char op;

public void setNumber(double fnum, double snum){
    this.fn = fnum;
    this.sn = snum;
}
public double getNumber1(){
    return fn;
}
public double getNumber2(){
    return sn;
}
public void setOper(char oper){
    this.op = oper;
}
public char getOper(){
    return op;
}
public void getAnswer(){
    double ans;
    switch (getOper()){
        case 'a': {
            ans = add(getNumber1(), getNumber2());
            ansOutput(ans);
            break;
        }case 'b': {
            ans = sub (getNumber1(), getNumber2());
            ansOutput(ans);
            break;
        }case 'c': {
            ans = mul (getNumber1(), getNumber2());
            ansOutput(ans);
            break;
        }case 'd': {
            ans = div (getNumber1(), getNumber2());
            ansOutput(ans);
            break;
        }default:
            System.out.println("--------------------------");
            System.out.println("Invalid choice of operator");
            System.out.println("--------------------------");
        }
    }
    public static double add(double x,double y){
        return x + y;
    }
    public static double sub(double x, double y){
        return x - y;
    }
    public static double mul(double x, double y){
        return x * y;
    }
    public static double div(double x, double y){
        return x / y;
    }

    public static void ansOutput(double x){
        System.out.println("----------- -------");
        System.out.printf("the answer is %.2f\n", x);
        System.out.println("-------------------");
    }
}

回答by Melquiades

From the docs

文档

In the Java programming language, every application must contain a main method whose signature is:

在 Java 编程语言中,每个应用程序都必须包含一个 main 方法,其签名为:

public static void main(String[] args)

The modifiers public and static can be written in either order (public static or static public), but the convention is to use public static as shown above. You can name the argument anything you want, but most programmers choose "args" or "argv".

修饰符 public 和 static 可以按任意顺序编写(public static 或 static public),但约定是使用如上所示的 public static。您可以随意命名参数,但大多数程序员选择“args”或“argv”。

As you say:

正如你所说:

error: missing method body, or declare abstract public static void main(String[] args); ^ this is what i got after i added it after the class name

错误:缺少方法体,或声明抽象 public static void main(String[] args); ^ 这是我在类名后添加后得到的

You probably haven't declared main with a body (as ';" would suggest in your error).

您可能没有使用主体声明 main (如 ';" 会在您的错误中提示)。

You need to have main method with a body, which means you need to add { and }:

你需要有一个带有主体的 main 方法,这意味着你需要添加 { 和 }:

public static void main(String[] args) {


}

Add it inside your class definition.

将它添加到您的类定义中。

Most importantly, please learn to read the docs AND error descriptions you are getting, they will help you many times to come.

最重要的是,请学会阅读文档和您收到的错误描述,它们将在未来多次帮助您。

回答by Sameer Sawla

My suggestions :

我的建议:

  • Keep the program modular. Keep the Calculate class in a separate Calculate.java file and create a new class that calls the main method. This would make the code readable.
  • For setting the values in the number, use constructors. Do not use like the methods you have used above like :

    public void setNumber(double fnum, double snum){ this.fn = fnum; this.sn = snum; }

    Constructors exists to initialize the objects.This is their job and they are pretty good at it.

  • Getters for members of Calculate class seem in place. But setters are not. Getters and setters serves as one important block in the bridge of efficient programming with java. Put setters for fnum and snum as well

  • In the main class, create a Calculate object using the new operator and the constructor in place.

  • Call the getAnswer() method with the created Calculate object.

  • 保持程序模块化。将Calculate 类保留在单独的Calculate.java 文件中,并创建一个调用main 方法的新类。这将使代码可读。
  • 要设置数字中的值,请使用构造函数。不要像上面使用的方法那样使用:

    public void setNumber(double fnum, double snum){ this.fn = fnum; this.sn = snum; }

    构造函数用于初始化对象。这是他们的工作,他们非常擅长。

  • 计算类成员的吸气剂似乎已经到位。但二传手不是。getter 和 setter 是使用 java 进行高效编程的桥梁中的一个重要模块。也为 fnum 和 snum 放置 setter

  • 在主类中,使用 new 运算符和构造函数创建一个计算对象。

  • 使用创建的Calculate 对象调用getAnswer() 方法。

Rest of the code looks fine to me. Be modular. You could read your program in a much better way.

其余的代码对我来说看起来不错。模块化。您可以以更好的方式阅读您的程序。

Here is my modular piece of code. Two files : Main.java & Calculate.java

这是我的模块化代码。两个文件:Main.java 和Calculate.java

Calculate.java

计算.java

public class Calculate {


private double fn;
private double sn;
private char op;

    public double getFn() {
        return fn;
    }

    public void setFn(double fn) {
        this.fn = fn;
    }

    public double getSn() {
        return sn;
    }

    public void setSn(double sn) {
        this.sn = sn;
    }

    public char getOp() {
        return op;
    }

    public void setOp(char op) {
        this.op = op;
    }



    public Calculate(double fn, double sn, char op) {
        this.fn = fn;
        this.sn = sn;
        this.op = op;
    }


public void getAnswer(){
    double ans;
    switch (getOp()){
        case '+': 
            ans = add(getFn(), getSn());
            ansOutput(ans);
            break;
        case '-': 
            ans = sub (getFn(), getSn());
            ansOutput(ans);
            break;
        case '*': 
            ans = mul (getFn(), getSn());
            ansOutput(ans);
            break;
        case '/': 
            ans = div (getFn(), getSn());
            ansOutput(ans);
            break;
        default:
            System.out.println("--------------------------");
            System.out.println("Invalid choice of operator");
            System.out.println("--------------------------");
        }
    }
    public static double add(double x,double y){
        return x + y;
    }
    public static double sub(double x, double y){
        return x - y;
    }
    public static double mul(double x, double y){
        return x * y;
    }
    public static double div(double x, double y){
        return x / y;
    }

    public static void ansOutput(double x){
        System.out.println("----------- -------");
        System.out.printf("the answer is %.2f\n", x);
        System.out.println("-------------------");
    }
}

Main.java

主程序

public class Main {
    public static void main(String args[])
    {
        Calculate obj = new Calculate(1,2,'+');
        obj.getAnswer();
    }
}

回答by Sanjukta

Where you have written the code

你写代码的地方

public class Main {
    public static void main(String args[])
    {
        Calculate obj = new Calculate(1,2,'+');
        obj.getAnswer();
    }
}

Here you have to run the class "Main" instead of the class you created at the start of the program. To do so pls go to Run Configuration and search for this class name"Main" which is having the main method inside this(public static void main(String args[])). And you will get your output.

在这里,您必须运行“Main”类而不是您在程序开始时创建的类。为此,请转到“运行配置”并搜索此类名称“Main”,它在此内部具有主要方法(public static void main(String args[]))。你会得到你的输出。

回答by Rohit kumar

Restart your IDE and everything will be fine

重新启动您的 IDE,一切都会好起来的

回答by Sudhan Nadar

you seem to have not created an main method, which should probably look something like this (i am not sure)

你似乎还没有创建一个主要的方法,它应该看起来像这样(我不确定)

  class RunThis
{
    public static void main(String[] args)
    {

    Calculate answer = new Calculate();
    answer.getNumber1();
    answer.getNumber2();
    answer.setNumber(answer.getNumber1() , answer.getNumber2());
    answer.getOper();
    answer.setOper(answer.getOper());
    answer.getAnswer();
    }
}

the point is you should have created a main method under some class and after compiling you should run the .class file containing main method. In this case the main method is under RunThis i.e RunThis.class.

关键是你应该在某个类下创建一个 main 方法,编译后你应该运行包含 main 方法的 .class 文件。在这种情况下,主要方法位于 RunThis 下,即 RunThis.class。

I am new to java this may or may not be the right answer, correct me if i am wrong

我是 Java 新手,这可能是也可能不是正确的答案,如果我错了,请纠正我