开发者

Java Guess Game [closed]

Closed. This question needs debugging details. It is not currently accepting answers.

Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.

Closed 7 years ago.

Improve this question

Hy,

I'm having problem with one code from a book Head first Java... (page 63/64)

I re-wrote code and it's not working. Can someone explain me what is that?

Error that im getting in Eclipse is:

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)

AND the code is:

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);
 开发者_Python百科       System.out.println("I'm guessing " + number);
    }
}

public class GameLauncher {
    public static void main (String[] imeStringa){
        GuessGame game = new GuessGame();
        game.startGame();
    }
}

THX


You can declare only one public class in a .java file


You can have only one public class per java file


In Java, you should have only 1 public class in a file. If you like to have multiple public classes then go for inner classes.There is an example here.

Why couldn't a single file have multiple public classes. I had the same question ringing in my mind, when I started with java. Whomever I ask, I got the same answer - "that's the way it works".

Regards, John


The error message tells you what is wrong: your code doesn't compile (you should see red error marks in the "Problems" view of Eclipse), because every public class in Java should be defined in a separate file having the name of the class :

public class GuessGame --> must be in GuessGame.java

public class Player --> must be in Player.java

The tree of packages must also match the tree of directories, starting from your source directory:

package com.foo.bar;
public class GuessGame  --> must be in com/foo/bar/GuessGame.java

Note: Eclipse lets you start an application even if it doesn't compile, but you shouldn't: it won't work.


You need to either put the Player and GameLauncher Classes in their own class file or make them inner classes by moving the bracket } just before the player class below the GameLauncher class. Also the game launcher class must be made static and the there is an extra bracket } after the GameLauncher class


As a lot of people mentioned above, you can't have more than one public class...so just make it where it isn't public for the classes except for the GuessGame one. Other than that, I just took of the last class and made the main inside the GuessGame since it is the public class, made startGame static and made the Player classes static as well. Then i called startGame(); in the main.

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);
  }
}


One pubic class per .java file.

For reference check your Head First Java book on page 7. (dive in A Quick Dip)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜