简体   繁体   English

错误:在Java类中找不到主要方法

[英]Error : Main method not found in java class

I am new to concepts of java. 我是Java概念的新手。 while preparing my first program of classes with objects i encountered a problem. 在准备我的第一个带有对象的类的程序时,我遇到了一个问题。 here is the code and the error..please resolve.. PROGRAM: 这是代码和错误。.请解决..程序:

class Fact
{
    private int i;
    private int n;
    private int fact;

    public Fact()
        { fact=1;
          i=1;
        }
    public Fact( int x)
        { n=x; }
    public void getAnswer()
        {
            while(i<=n)
            {fact=fact*i;
                i++;}
            System.out.println(fact);
        }
}
class FactMain
{
    public static void main(String dt[])
    {
    Fact obj= new Fact(6);
    obj.getAnswer();
    }
}

ERROR: 错误:

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

just change your Parameterized constructor to this 只需将您的Parameterized构造函数更改为此

public Fact(int x) {
    fact = 1;
    i = 1;
    n = x;
}

because you declare factorial in default constructor and you are not calling it. 因为您在默认构造函数中声明了阶乘 ,但是您没有调用它。 So, 0 is assigned to factorial and then you r trying to multiply it. 因此,将0分配给阶乘,然后尝试将其乘以。 Which makes no sense. 这没有任何意义。

You have saved your file as Fact.java . 您已将文件另存为Fact.java So java is trying to find the main class in Fact. 因此,java尝试在Fact中查找主类。 Save your file as FactMain.java It should work. 将文件另存为FactMain.java 。应该可以。

Your main method is in FactMain.java , but you are saving a file as Fact.java . 您的主要方法在FactMain.java中 ,但是您正在将文件另存Fact.java

You will need to save the file as FactMain.java as JVM expects main to be in the same class as the name of .java file. 您将需要将文件另存为FactMain.java,因为JVM希望main与.java文件的名称位于同一类中。

Rename the class file name Fact.java to FactMain.java . 将类文件名Fact.java重命名为FactMain.java

private int fact;

public Fact()
    { fact=1;
      i=1;
    }
public Fact( int x)
    { n=x; }

Note, your default constructor set fact but constructor Fact( int x) set n . 注意,默认构造函数设置fact但构造函数Fact( int x)设置n Hence fact is 0 . 因此, fact0 So your output is 0 too. 因此您的输出也是0

Solution: 解:

public Fact(int x) {
    fact = 1;
    i = 1;
    n = x;
}

Or, 要么,

public Fact(int x) {
    this(); // default constructor
    n = x;
}

Here is the complete solution: 这是完整的解决方案:

Create a single class file named FactMain.java , then paste the following code: 创建一个名为FactMain.java class文件,然后粘贴以下代码:

class Fact {
    private int i;
    private int n;
    private int fact;

    public Fact() {
        fact = 1;
        i = 1;
    }

    public Fact(int x) {
        this();
        n = x;
    }

    public void getAnswer() {
        while (i <= n) {
            fact = fact * i;
            i++;
        }
        System.out.println(fact);
    }
}

class FactMain {
    public static void main(String[] dt) {
        Fact obj = new Fact(6);
        obj.getAnswer();
    }
}

You have defined your main class in FactMain and most probably after compilation while running you're trying to execute 您已经在FactMain中定义了主类,并且很有可能在运行时编译后尝试执行

java Fact Java事实

And hence you got the error because there is no main method in Fact class. 因此,由于在Fact类中没有main方法,因此出现了错误。 Once you compile the .java file you will get two class files Fact.class and FactMain.class so you should execute 编译.java文件后,您将获得两个类文件Fact.class和FactMain.class,因此应执行

java FactMain Java FactMain

Move the FactMain class to FactMain.java FactMain类移动到FactMain.java

FactMain.java FactMain.java

public class FactMain
{
    public static void main(String dt[])
    {
        Fact obj= new Fact(6);
        obj.getAnswer();
    }
}

Allow the Fact class to remain in the Fact.java file 允许Fact类保留在Fact.java文件中

Fact.java Fact.java

public class Fact {

    private int i;
    private int n;
    private int fact;

    public Fact() {
        fact = 1;
        i = 1;
    }

    public Fact(int x) {
        this();
        n = x;
    }

    public void getAnswer() {
        while (i <= n) {
            fact = fact * i;
            i++;
        }
        System.out.println(fact);
    }
}

Compile the classes... 编译类...

javac {package path}\FactMain.java

Run the main class 跑主班

java {package path}.FactMain

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

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