将高尔夫球得分变成阵列

时间:2014-06-12 15:00:43

标签: java arrays

嘿伙计们我正在尝试制作一个模拟高尔夫记分卡的程序。我被阵列困住了...... 我有一个洞[] {0,1,......,17}计算一个18洞球场。但是对于数组中的每个元素,我认为我将需要另一个数组来获取用户对该洞的分数。 我需要帮助增加漏洞得分(h1S,h2S,h3S ......),因为现在h1S将被重用于每个洞,所以它只是重写自己?大声笑我还好,请帮帮我。

static int hole[] = new int[18];
{

hole[0] = h1S; // hole Scores..
hole[1] = h2S;
hole[2] = h3S;
hole[3] = h4S;
hole[4] = h5S;
hole[5] = h6S;
hole[6] = h7S;
hole[7] = h8S;
hole[8] = h9S;
hole[9] = h10S;
hole[10] = h11S;
hole[11] = h12S;
hole[12] = h13S;
hole[13] = h14S;
hole[14] = h15S;
hole[15] = h16S;
hole[16] = h17S;
hole[17] = h18S;

}

public static void test1() 
{
    Scanner input = new Scanner(System.in);

    if (numPlayers == 1)
    {
        for (int holeNumber = 1; holeNumber < 19; holeNumber++)
        {
            System.out.println(player1 + ", Enter your hole " + holeNumber + " score.");
            h1S = input.nextInt();// i need increment h1S somehow *******************
        }
    }

    else if (numPlayers == 2)
    {
        for (int holeNumber = 1; holeNumber < 19; holeNumber++)
        {
            System.out.println(player1 + ", Enter your hole " + holeNumber + " score.");
            h1S = input.nextInt();

            System.out.println(player2 + ", Enter your hole " + holeNumber + " score.");
            h1S = input.nextInt();
        }
    }

    else if (numPlayers == 3)
    {
        for (int holeNumber = 1; holeNumber < 19; holeNumber++)
        {
            System.out.println(player1 + ", Enter your hole " + holeNumber + " score.");
            h1S = input.nextInt();

            System.out.println(player2 + ", Enter your hole " + holeNumber + " score.");
            h1S = input.nextInt();

            System.out.println(player3 + ", Enter your hole " + holeNumber + " score.");
            h1S = input.nextInt();
        }
    }

    else if (numPlayers == 4) 
    {
        for (int holeNumber = 1; holeNumber < 19; holeNumber++)
        {
            System.out.println(player1 + ", Enter your hole " + holeNumber + " score.");
            h1S = input.nextInt();

            System.out.println(player2 + ", Enter your hole " + holeNumber + " score.");
            h1S = input.nextInt();

            System.out.println(player3 + ", Enter your hole " + holeNumber + " score.");
            h1S = input.nextInt();

            System.out.println(player4 + ", Enter your hole " + holeNumber + " score.");
            h1S = input.nextInt();
        }
    }


}// end test1

谢谢你们。

1 个答案:

答案 0 :(得分:3)

你确实需要一个2D数组(如果你想保持数组得分),但你可以以编程方式生成它

int[][] score;

public Scorecard(int players){
  score = new int[18][players];
}

然后,为了保持分数,只需记录score[hole][playernum] = score

将其视为一个网格,x轴为洞,y为玩家。此方法还允许您在每个笔划进行记录,因为您可以使用score[hole][playernum]++

递增它