gpt4 book ai didi

java - 将高尔夫分数放入数组中

转载 作者:行者123 更新时间:2023-11-29 05:22:18 25 4
gpt4 key购买 nike

大家好,我正在尝试制作一个模拟高尔夫记分卡的程序。我坚持使用数组...我有一个 hole[] {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

谢谢你们。

最佳答案

你确实需要一个二维数组(如果你想用数组记分)但你可以通过编程生成它

int[][] score;

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

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

将其想象成一个网格,x 轴是球洞,y 轴是球员。此方法还允许您在每次击球时进行记录,因为您可以使用 score[hole][playernum]++

递增它

关于java - 将高尔夫分数放入数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24187709/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com