【问题标题】:Calculating wins/losses, winning percentage and total winnings计算赢/输、赢率和总赢款
【发布时间】:2016-08-22 17:38:10
【问题描述】:

我只是在练习 Java,对此还很陌生。我只是想创建一个随机数生成器程序来跟踪玩家的赢、输、赢率和总赢额。该程序的逻辑是玩家在每个会话中获得 3 次机会,并且计算机会生成一个随机数,玩家需要猜测或者应该匹配。

我目前有三个类:Game(包含主要逻辑)、Player(应该有赢/输等。这是我的猜测)和 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();
    }
}

【问题讨论】:

    标签: java bluej


    【解决方案1】:

    根据 OO 设计,gamesWon、gamesLost、totalWinnings 和 winPercent 等属性应该只在 Player 类中(正确)。

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

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

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

    【讨论】:

    • 感谢您的回答。 1-100 是无意的,已修复。
    【解决方案2】:

    我已修改构造函数以接受名称。

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

    Player 是一个消耗 Game 的对象。所以最好在 Game 中编写逻辑并使 Player 尽可能简单。

    【讨论】:

    • 谢谢!再次确认,所有的计算都可以在 Game 类中进行,对吧?我不需要在 Player 类中放任何东西?
    • JavaUser 从默认构造函数调用另一个构造函数有什么用。如果你想实现这一点,只需一个构造函数就足以将所有默认值设置为类中的变量。
    猜你喜欢
    • 2022-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-12
    • 2017-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多