我是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;
}
}
}
}
程序的名称与类的名称相同,我发现解决方案一旦我在类播放器和游戏中删除公共,程序工作没有错误但仍然我无法理解的含义错误 。请帮助我 !!
答案 0 :(得分:0)
公共类必须拥有自己的同名文件。但是,如果它不公开,则可以将其放在具有不同名称的文件中