我試圖將 i 的隨機總和放入四個變數 scoreTotalOne、scoreTotalTwo、scoreTotalThree 和 scoreTotalFour。我在輸出中沒有得到正確的答案?任何幫助將不勝感激。
//array for par
int[] parArray = { 4, 3, 4, 4, 5, 4, 5, 3, 4, 4, 3, 5, 4, 4, 5, 4, 3, 4};
//multi-rectangular array 18 holes, 4 golfers
int[,] arr = new int[18, 4];
//generate random scores for holes/golfers
Random randomScores = new Random();
Console.WriteLine("Hole Par Golfer 1 Golfer 2 Golfer 3 Golfer 4");
int scoreTotalOne = 0;
int scoreTotalTwo = 0;
int scoreTotalThree = 0;
int scoreTotalFour = 0;
for (int i = 0; i < 9; i )
{
Console.Write((i 1) "\t");
Console.Write(parArray[i] "\t");
for (int j = 0; j < 4; j )
{
arr [i, j] = randomScores.Next(parArray [i] - 2, parArray [i] 3);
Console.Write(arr[i, j] "\t");
scoreTotalOne = arr[i, j];
scoreTotalTwo = arr[i, j];
scoreTotalThree = arr[i, j];
scoreTotalFour = arr[i, j];
}
Console.WriteLine();
}
Console.WriteLine("Front" " " scoreTotalOne " " scoreTotalTwo " " scoreTotalThree " " scoreTotalFour);
uj5u.com熱心網友回復:
您需要一個陣列,而不是四個單獨的變數。這將使得在內部回圈中只選擇(正確的)一個要更新的總數變得容易。否則,所有四個分數將始終得到最終高爾夫球手的分數,因為內回圈當前設定了所有四個變數。它看起來像這樣:
int[] holePars = { 4, 3, 4, 4, 5, 4, 5, 3, 4, 4, 3, 5, 4, 4, 5, 4, 3, 4};
int[,] holeScores = new int[18, 4];// 18 holes, 4 golfers
int[] totals = {0, 0, 0, 0};
var randomScores = new Random();
Console.WriteLine("Hole Par Golfer 1 Golfer 2 Golfer 3 Golfer 4");
for (int i = 0; i < 9; i )
{
Console.Write($" {i 1}{holePars[i],4} ");
for (int j = 0; j < 4; j )
{
holeScores[i,j] = randomScores.Next(holePars[i] - 2, holePars[i] 3);
Console.Write($"{holeScores[i,j],-9}");
totals[j] = holeScores[i,j];
}
Console.WriteLine();
}
Console.WriteLine($"Front {totals[0]} {totals[1]} {totals[2]} {totals[3]}");
在這里看到它的作業:
https://dotnetfiddle.net/6XSLES
我喜歡你為獲得隨機分數而偏離標準桿所做的事情。如果你真的想讓它看起來更像一場真正的高爾夫比賽,你還可以為分數創建權重,這樣高爾夫球手更有可能最終接近標準桿。這可能看起來像這樣:
https://dotnetfiddle.net/qoUOWf
上面鏈接中的代碼使您在雙鷹上擊中標準桿的可能性增加了 4 倍以上,而原來的代碼完全是隨機的。
此外,在一開始就對高爾夫球手的技能進行加權會很有趣,因此您不太可能讓同一個高爾夫球手同時獲得三柏忌和老鷹。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/491715.html