-1

i am new to programming in java and dont know the meaning of these errors :

Gamelauncher.java:11: error: class player is public, should be declared in a file named player.java
public class player{
       ^
Gamelauncher.java:30: error: class playgame is public, should be declared in a file named playgame.java
public class playgame{
       ^

2 errors

source code :

public class Gamelauncher{

public static void main(String[] args){


    playgame game = new playgame();
    game.play();
                                        }
}

public class player{


    int number;

    public int guessnumber(){

    number=(int)(Math.random()*10);

    return number;

    }




}


public class playgame{

 public void play(){

    while(true){
player p1 = new player(),p2 = new player(),p3 = new player();
int n1,n2,n3;
boolean player1=false,player2=false,player3=false;
int targetnumber;

targetnumber=(int)(Math.random() *10);

System.out.println("target number is :" + targetnumber);


    n1=p1.guessnumber();
    System.out.println("player 1 is guessing " + n1);

    n2=p2.guessnumber();
    System.out.println("player 2 is guessing " + n2);

    n3=p3.guessnumber();
    System.out.println("player 3 is guessing " + n3);


    if(targetnumber== n1){

        player1=true;

    }

    if(targetnumber== n2){

        player2=true;

    }

    if(targetnumber== n3){

        player3=true;

    }


    if(player1 || player2 || player3){

        System.out.println("we have winner");
        if(player1){
            System.out.println("winner is player 1" );
        }
        if(player2){
            System.out.println("winner is player2" );

        }

        if(player3){
            System.out.println("winner is player 3 ");

        }
        break;

    }

 }

}



}

Name of the program is same as the name of the class and i found the solution also as soon as i remove public in classes player and playgame, program worked with no errors but still i could not understand the meaning of the error . pls help me !!

1 Answers1

0

Public classes have to have their own files with the same name. If it's not public, though, it can be put in a file with a different name,

kirbyquerby
  • 735
  • 5
  • 16