我只是在练习Java,对此还很陌生。我只是试图创建一个随机数生成器程序,该程序可以跟踪玩家的获胜,失败,获胜百分比和总奖金。该程序的逻辑是,玩家每次会话获得3次机会,并且计算机会生成一个随机数,玩家需要猜测或应该匹配。

我目前有三个类:游戏(拥有主要逻辑),玩家(应该具有赢/输等等。是我猜的)和RandomNumberGenerator(生成随机数)。

我从程序开始,但是对于是否需要在游戏者类中将gamesWon,gamesLost,totalWinnings,winPercent声明为单独的变量感到困惑?任何帮助,将不胜感激。

到目前为止,这是玩家类别:

public class Player {
    private String name;
    private int totalWinnings;
    private int gamesWon;
    private int gamesLost;

    public Player() {
        this.name = "default";
        this.totalWinnings = 0;
        this.gamesWon = 0;
        this.gamesLost = 0;
    }

    public Player(String name) {
        this.name = "default";
        this.totalWinnings = 0;
        this.gamesWon = 0;
        this.gamesLost = 0;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getGamesWon() {
        return gamesWon;
    }

    public void setGamesWon(int gamesWon) {
        this.gamesWon += gamesWon;
    }

    public int getGamesLost() {
        return gamesLost;
    }

    public void setGamesLost(int gamesLost) {
        this.gamesLost += gamesLost;
    }

    public void setTotalWinnings(int totalWinnings) {
        this.totalWinnings += totalWinnings;
    }

    public int getTotalWinnings() {
        return totalWinnings;
    }
}


游戏类别:

public class Game {

    private Player player;
    private LuckyNumberGenerator lng;

    public Game() {
        player = new Player();
        lng = new LuckyNumberGenerator();
    }

    private void eventLoop() {
        Scanner scanner = new Scanner(System.in);
        int choice = 0;
        boolean exit = false;
        while (!exit) {
            System.out.println("Welcome to the Guessing Game");
            System.out.println("==============================");
            System.out.println("(1) Set Up New Player");
            System.out.println("(2) Play One Round");
            System.out.println("(3) Player Win Statistics");
            System.out.println("(4) Display Game Help");
            System.out.println("(5) Exit Game");
            System.out.println("Choose an option: ");

            try {
                choice = Integer.parseInt(scanner.nextLine());
                if (choice < 1 || choice > 5) {
                    System.err.println("Error : Choose an option between 1 and 5");
                    choice = 0;
                }
            } catch (NumberFormatException e) {
                System.err.println("Error : Choose an option between 1 and 5");
                choice = 0;
            }

            switch (choice) {
            case 1:
                createNewPlayer(scanner);
                break;
            case 2:
                guessNumber(scanner);
                break;
            case 3:
                printStatistics();
                break;
            case 4:
                printHelp();
                break;
            case 5:
                exit = true;
            }
        }
        scanner.close();
    }
}

最佳答案

根据OO Design,像gameWon,gamesLost,totalWinnings和winPercent之类的属性应仅在Player类中(正确)。

您有一个与Default或Zero参数化构造函数相同的参数化构造函数。您没有使用传递name(String)作为参数,因为您将每个对象的名称设置为“ default”相同的字符串,而不管传递的值如何。

要求用户在1到100(选择)之间选择是没有意义的,因为您只有5个案例可以切换。

您不应将扫描仪对象传递给其他方法,因为它仅应用于扫描数据并在使用后立即将其关闭。
您可以定义所需的任何方法的扫描仪对象。

10-07 16:08