当前位置: 首页 > 知识库问答 >
问题:

这个错误在java中意味着什么?[副本]

皇甫聪
2023-03-14

我是java编程新手,不知道这些错误的含义:

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

源代码:

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;

    }

 }

}



}

程序的名称与类的名称相同,我找到了解决方案。当我删除类中的public player和playgame时,程序没有出现错误,但我仍然无法理解错误的含义。请帮帮我

共有1个答案

冯阳成
2023-03-14

公共类必须有自己的同名文件。但是,如果它不是公共文件,则可以将其放在具有不同名称的文件中,

 类似资料: