Getting error: class, interface, or enum expected... everything seems to be declared properly not sure why it's happening

Getting error: class, interface, or enum expected... everything seems to be declared properly not sure why it's happening

import java.util.Random;

/** Generate 10 random integers in the range 0..99. */
public final class RandomInteger {

  public static final void main(String... aArgs){
    System.out.println("Generating random number");

    //note a single Random object is reused here
    Random randomGenerator = new Random();
    int randomInt = randomGenerator.nextInt(1000000);
    int randomInt2 = randomGenerator.nextInt(1000000);

    System.out.println("generated " +  randomInt + " and " + randomInt2);


    if (isPrime(randomInt)==true)
    {
        System.out.println("it's prime");
    }

    else 
    {
        System.out.println("it's not ");
    }


    System.out.println("Done.");

    System.exit(0);
  }
}





 public static boolean isPrime(int random) 
{



    //check if n is a multiple of 2
    if (random%2==0) return false;
    //if not, then just check the odds
    for(int i=3;i*i<=random;i+=2) 
    {
        if(random%i==0)
        {
            return false;
        }
        else
        {
        return true;
        }
    }
}

您的 class 中有 isPrime 方法,它需要在 class 的范围内。所以在main方法之前定义它。

类似于:

public final class RandomInteger {
  public static boolean isPrime(int random) {
      ....
  }
  public static final void main(String... aArgs){
      ....