计算赢/输,胜率和总奖金

时间:2016-08-22 08:58:06

标签: java bluej

我只是在练习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();
    }
}

1 个答案:

答案 0 :(得分:1)

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

public Player() {
        Player("default");
    }

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

玩家是一个消耗游戏的对象。因此,最好在Game中编写逻辑并尽可能简化Player。