B
B
Bill_Kim_912019-11-30 20:44:28
Java
Bill_Kim_91, 2019-11-30 20:44:28

Class X is public and must be declared in a file named X?

Hello.
I figured out the error itself. Removed public, left it only for GameLauncher and everything compiled and started.
The question is different.
What is the right class to declare public?
And why is there an example with errors in the book (the code was from the book)? Perhaps due to the fact that the book is on java 5.
I would be very grateful if you explain on what basis to declare a class public and why it happened from the examples of the book.
My JSE
java version "13.0.1" 2019-10-15
Java(TM) SE Runtime Environment (build 13.0.1+9)
Java HotSpot(TM) 64-Bit Server VM (build 13.0.1+9, mixed mode , sharing)
Error while compiling. I use javac without any IDE.
GameLauncher.java:1: error: class GuessGame is public, should be declared in a file named GuessGame.java
public class GuessGame {
^
GameLauncher.java:61: error: class Player is public, should be declared in a file named Player. java
public class Player {
^
2 errors

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 targetNumber = (int) (Math.random() * 10);
        System.out.println("Я загадываю число от 0 до 9...");
 
        while(true) {
            p1.guess();
            p2.guess();
            p3.guess();
 
            guessp1 = p1.number;
            System.out.println("Первый игрок думает что это " + guessp1);
 
            guessp2 = p2.number;
            System.out.println("Второй игрок думает что это " + guessp2);
            
            guessp3 = p3.number;
            System.out.println("Третий игрок думает что это " + guessp3);
 
            if (guessp1 == targetNumber) {
                p1isRight = true;
            }
            if (guessp2 == targetNumber) {
                p2isRight = true;
            }
            if (guessp3 == targetNumber) {
                p3isRight = true;
            }
 
            if (p1isRight || p2isRight || p3isRight) {
 
                System.out.println("У нас есть победитель!");
                System.out.println("Первый игрок угадал? " + p1isRight);
                System.out.println("Второй игрок угадал? " + p2isRight);
                System.out.println("Третий игрок угадал? " + p3isRight);
                System.out.println("Конец игры");
                break;
            } else {
                System.out.println("Игроки должны попробовать ещё раз.");
            }
        }
    }
}
 
public  class Player {
    int number = 0;
    public void guess() {
        number = (int)(Math.random() * 10);
        System.out.println("Думаю что это число " + number);
    }
}
 
public class GameLauncher {
    public static void main(String[] args) {
        GuessGame game = new GuessGame();
        game.startGame();
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Oleg Carp, 2019-12-04
@Rebel-Cat

one file - one public class, it is better to use the IDE right away so as not to waste time on such errors

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question