HY,
我遇到一本书中的一个代码问题首先是Java ... (第63/64页)
我重新编写了代码但它没有用。有人可以解释一下那是什么吗?
我在Eclipse中遇到的错误是:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The public type GuessGame must be defined in its own file
The public type Player must be defined in its own file
at GuessGame.<init>(GameLauncher.java:1)
at GameLauncher.main(GameLauncher.java:73)
并且代码为:
public class GuessGame {
Player p1;
Player p2;
Player p3;
public void startGame(){
p1 = new Player();
p2 = new Player();
p3 = new Player();
int guessp1 = 0;
int guessp2 = 0;
int guessp3 = 0;
boolean p1isright = false;
boolean p2isright = false;
boolean p3isright = false;
int BrojKojiTrebaPogodit = (int)(Math.random() * 10);
System.out.println ("Razmisljam o broju izmedju 0 i 9 ... ");
while (true){
System.out.println ("Zamisljeni broj je " + BrojKojiTrebaPogodit);
p1.guess();
p2.guess();
p3.guess();
guessp1 = p1.number;
System.out.println ("Player P1 guessed " + guessp1);
guessp2 = p2.number;
System.out.println ("Player P2 guessed " + guessp2);
guessp3 = p3.number;
System.out.println ("Player P3 guessed " + guessp3);
if (guessp1 == BrojKojiTrebaPogodit){
p1isright = true;
}
if (guessp2 == BrojKojiTrebaPogodit){
p2isright = true;
}
if (guessp3 == BrojKojiTrebaPogodit){
p3isright = true;
}
if (p1isright || p2isright || p3isright) {
System.out.println ("We have winner!");
System.out.println ("Is P1 got it right? " + p1isright);
System.out.println ("Is P2 got it right? " + p2isright);
System.out.println ("Is P3 got it right? " + p3isright);
System.out.println ("Game is over");
break;
}
else {
System.out.println ("Players will have to try again!");
}
}
}
}
public class Player {
int number = 0;
public void guess() {
number = (int) (Math.random()*10);
System.out.println("I'm guessing " + number);
}
}
public class GameLauncher {
public static void main (String[] imeStringa){
GuessGame game = new GuessGame();
game.startGame();
}
}
THX
答案 0 :(得分:6)
您只能在.java文件中声明一个公共类
答案 1 :(得分:5)
每个java文件只能有一个公共类
答案 2 :(得分:1)
在Java中,文件中只能有一个公共类。如果你想拥有多个公共类,那就选择内部类。有an example here。
为什么单个文件不能有多个公共类。当我开始使用java时,我心里也有同样的问题。无论我问谁,我都得到了相同的答案 - “这就是它的工作方式”。
此致 约翰
答案 3 :(得分:0)
错误消息告诉您错误:您的代码无法编译(您应该在Eclipse的“Problems”视图中看到红色错误标记),因为Java中的每个公共类都应该在一个单独的文件中定义班级名称:
public class GuessGame --> must be in GuessGame.java
public class Player --> must be in Player.java
包树也必须与目录树匹配,从源目录开始:
package com.foo.bar;
public class GuessGame --> must be in com/foo/bar/GuessGame.java
注意:Eclipse允许你启动一个应用程序,即使它没有编译,但你不应该:它不会工作。
答案 4 :(得分:0)
你需要将Player和GameLauncher类放在他们自己的类文件中,或者通过在GameLauncher类下面的玩家类之前移动括号}来使它们成为内部类。游戏启动器类必须是静态的,并且在GameLauncher类之后还有一个额外的括号}
答案 5 :(得分:0)
正如上面提到的很多人,你不能拥有一个以上的公共课......所以只要把它放在除了GuessGame之外的类不公开的地方。除此之外,我刚刚接受了最后一个类,并将其作为GuessGame的主要部分,因为它是公共类,使得startGame静态并使Player类也是静态的。然后我打电话给startGame();在主要。
public class GuessGame {
static Player p1;
static Player p2;
static Player p3;
public static void main(String[] args) {
startGame();
}
public static void startGame(){
p1 = new Player();
p2 = new Player();
p3 = new Player();
int guessp1 = 0;
int guessp2 = 0;
int guessp3 = 0;
boolean p1isright = false;
boolean p2isright = false;
boolean p3isright = false;
int BrojKojiTrebaPogodit = (int)(Math.random() * 10);
System.out.println ("Razmisljam o broju izmedju 0 i 9 ... ");
while (true){
System.out.println ("Zamisljeni broj je " + BrojKojiTrebaPogodit);
p1.guess();
p2.guess();
p3.guess();
guessp1 = p1.number;
System.out.println ("Player P1 guessed " + guessp1);
guessp2 = p2.number;
System.out.println ("Player P2 guessed " + guessp2);
guessp3 = p3.number;
System.out.println ("Player P3 guessed " + guessp3);
if (guessp1 == BrojKojiTrebaPogodit){
p1isright = true;
}
if (guessp2 == BrojKojiTrebaPogodit){
p2isright = true;
}
if (guessp3 == BrojKojiTrebaPogodit){
p3isright = true;
}
if (p1isright || p2isright || p3isright) {
System.out.println ("We have winner!");
System.out.println ("Is P1 got it right? " + p1isright);
System.out.println ("Is P2 got it right? " + p2isright);
System.out.println ("Is P3 got it right? " + p3isright);
System.out.println ("Game is over");
break;
}
else {
System.out.println ("Players will have to try again!");
}
}
}
}
class Player {
int number = 0;
public void guess() {
number = (int) (Math.random()*10);
System.out.println("I'm guessing " + number);
}
}
答案 6 :(得分:0)
每个.java文件一个公共类。
有关参考,请参阅第7页上的 Head First Java 一书。 (潜入 A Quick Dip )