我不知道如何解决此错误,代码编译但无法正常运行



所以代码可以编译,但是当我尝试运行代码时,我收到此错误。

public class Die
{
private int randomValue;
public Die()
{
randomValue = ((int)(Math.random() * 100)%6 + 1);
}
public int getValue()
{
return randomValue;
}
}

任何帮助将不胜感激。

错误:在类 Die 中找不到主方法,请将主方法定义为: 公共静态空隙主(字符串[] 参数( 或者 JavaFX 应用程序类必须扩展 javafx.application.Application

在java中,每个应用程序都必须有一个main方法,即条目执行。

在这种情况下,我假设您要创建一个 Die 类的对象,然后打印随机生成的值,为此,请按下一种方式:

在类中创建一个 main 方法:

public class Die{
private int randomValue;
public Die(){
this.randomValue = ((int)(Math.random() * 100)%6 + 1);
}
public int getValue(){
return this.randomValue;
}
public static void main(String[] args){
//Create a new object of Die class
Die dieObject = new Die();
//Print random value ussing getter method
System.out.println("The random value is: " + dieObject.getValue());
}
}

所有 Java 程序都需要一个名为mainstatic方法才能启动。通过快速的谷歌搜索,您可以找到大量这样的链接。但是,您的类可以编译,因为它没有编译错误,但由于没有main方法而无法运行。

在您的情况下,我想您想做这样的事情:

public static void main(String[] arguments)
{
Die die = new Die();
}

另一个很好读的是为什么Java主方法是静态的。

最新更新