-3

そのため、プログラムはボウリング ゲームを呼び出します。何人のプレーヤーがいるかを尋ねると、すべてのプレーヤーが 2 スローを取得してピンを 10 個倒します (乱数によってシミュレートされます)。1投目で全員倒せば20点、2投目で15点、それ以外は1投+2投。

私にとってトリッキーな部分は、プログラムが全員のスコアを追跡している間に、各プレーヤーに交互のフレーム (2 スロー = 1 フレーム、プレーヤーごとに最大 10 フレーム) を取得することでした! 私はこれでそれを持っていると思っていましたが、ランダムが加算されて出力が下部に表示されます

import java.util.Scanner;

public class Bowling {

    public static void main(String [] args){
        Scanner input = new Scanner (System.in);
        Game aNew = new Game();
        int player;
        int i;
        int j;
        int nPlay;



        System.out.print("How many players are there?: ");
        nPlay = input.nextInt();
            for (j = 1; j<= 10; j++) {
                for (i = 1; i <= nPlay; i++ ){
                     player = i; // i tried player i = new Player() but get error "value
                     aNew.getScore(player); // already used in scope""
                }
            }

ゲームクラス:

import java.lang.Math;

    public class Game {
    int score = 0;
    int player;
    int ran1;
    int ran2;

    public Game() {
    }

    public int getScore(int player){
        ran1 = (int) (9 * Math.random());
        ran2 = (int) (((10 - ran1)) * Math.random());

        if (ran1 == 10){
            score += 20;
        } else if (ran1 + ran2 == 10){
            score += 15;
        } else {
            score += ran1 + ran2;
        }   System.out.println("Player " + player + " score is: " + score + "\n");
        System.out.println("ran1: " + ran1 + "  ran2: " + ran2);
        return score;
    }
}

プレイヤークラス:

public class Player {
   /** this class is not doing anything, however i would like for an object to store a score for every player to keep track if that is possible?*/ 
    int score;

    public Player (){
        int score;
    }
}

出力: (最後のプレーヤーが最高値を記録するたびに、それが機能していないことに気付かなかったことは疑わしいです!)

How many players are there?: 2

Player 1 score is: 2

ran1: 1  ran2: 1
Player 2 score is: 11

ran1: 8  ran2: 1
Player 1 score is: 16

ran1: 5  ran2: 0
Player 2 score is: 21

ran1: 4  ran2: 1
Player 1 score is: 29

ran1: 8  ran2: 0
Player 2 score is: 35

ran1: 5  ran2: 1
Player 1 score is: 40

ran1: 3  ran2: 2
Player 2 score is: 47

ran1: 6  ran2: 1
Player 1 score is: 56

ran1: 8  ran2: 1
Player 2 score is: 61

ran1: 5  ran2: 0
Player 1 score is: 68

ran1: 7  ran2: 0
Player 2 score is: 77

ran1: 8  ran2: 1
Player 1 score is: 85

ran1: 4  ran2: 4
Player 2 score is: 90

ran1: 4  ran2: 1
Player 1 score is: 97

ran1: 7  ran2: 0
Player 2 score is: 103

ran1: 3  ran2: 3
Player 1 score is: 107

ran1: 4  ran2: 0
Player 2 score is: 114

ran1: 3  ran2: 4
Player 1 score is: 121

ran1: 7  ran2: 0
Player 2 score is: 125

ran1: 0  ran2: 4
4

2 に答える 2