-1

So the code will compile but when i try to run the code i get this error.

    public class Die
{
  private int randomValue;

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

Any help would be greatly appreciated.

Error: Main method not found in class Die, please define the main method as: public static void main(String[] args) or a JavaFX application class must extend javafx.application.Application

2 Answers2

0

All Java programs require a static method named main in order to start. With a quick google search you can find tons of links like this one. However, your class can be compiled since it has no compile errors but it cannot run because there is no main method.

In your case I guess you want to do something like this:

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

Also a nice-to-read is Why is the Java main method static.

George Z.
  • 6,024
  • 4
  • 18
  • 33
0

In java every application must have a main method, which is the entry execution.

In this case i supposed you want to create an object of Die class and then print the random generated value, for this make it the next way:

Create a main method in the class:

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());
    }
}