我在保龄球计划中的结果也与我想要的不符

时间:2019-02-14 02:59:42

标签: java

我创建了一个保龄球比赛程序,该程序给出了您输入的球员人数的结果。我的问题是,我的输出始终是相同的数字,加上每一轮球员1和球员2的得分始终为0。

我尝试移动随机数生成器,但是没有用。

客户代码:

import java.util.Scanner;

public class Bowling {
    public static void main(String[]args){
        int playerAmount, flag = 0;

        Scanner input = new Scanner(System.in);

        System.out.print("Enter the number of players in the game: ");
        playerAmount = input.nextInt();

        Bowlers[] bowlers = new Bowlers[playerAmount];

        for (int i = 0; i < playerAmount; i++) {
            bowlers[i] = new Bowlers(i);
        }

        for (int i = 0; i < 10; i++){
            for(int j = 0; j < playerAmount; j++){
                flag = 0;
                while(flag != 2){
                    bowlers[j].setPinKnock();
                    bowlers[j].changePinAmount();
                    if(bowlers[j].getPinAmount() == 0){
                        flag ++;
                    }
                    flag++;
                }
                bowlers[j].changeScore();
            }
                for(int g = 0; g < playerAmount; g++){
                    System.out.println(bowlers[g]);
                }
        }
    }
}

保龄球代码:

public class Bowlers {
    private int score, pins,tag,pinKnock;
    public Bowlers(int i) {
        score = 0;
        pins = 10;
        tag = i;
        pinKnock = 0;
    }

    public void setPinKnock(){
        pinKnock = (int)(Math.random() * ((pins - 1) + 1)) + 1;
    }

    public void changePinAmount(){
        pins -= pinKnock;
    }

    public int getPinAmount(){
        return pins;
    }

    public void changeScore(){
        if(pins == 0 && pinKnock == 10){
            score += 20;
        }else if(pins != 0){
            score += (10 - pins);
        }else{
            score += 15;
        }
    }

    public String toString(){
        return "Player" + tag + " Score: " + score;
    }
}

这是编辑后的输出:

Player0得分:9

Player1得分:9

Player2得分:8

Player0得分:24

Player1得分:24

Player2得分:23

Player0得分:36

Player1得分:36

Player2得分:35

Player0得分:49

Player1得分:49

Player2得分:47

Player0得分:64

玩家1得分:63

Player2得分:60

Player0得分:79

Player1得分:75

Player2得分:74

Player0得分:93

玩家1得分:87

Player2得分:87

Player0得分:106

玩家1得分:101

Player2得分:99

Player0得分:119

Player1得分:113

Player2得分:112

Player0得分:132

Player1得分:126

Player2得分:124

0 个答案:

没有答案