我正在爲我的課寫這個高爾夫課程,它需要一個每行和每行有5個數字的.txt文件。每一排中的第一個數字就是該洞的標準桿。其他四個數字是針對每個玩家的。如何在Java中動態創建具有不同名稱的指定數量的數組
儘管如此,我對這個程序有自己的看法。我寫了另一個程序來創建帶有隨機數字的.txt文件。輸出取決於用戶輸入的玩家數量。
無論如何,我已經獲得了.txt生成器,並且我已經獲得了高爾夫計劃來精確計算有多少玩家。我無法弄清楚的是如何創建具有不同名稱的數組。我希望每個陣列是int player1[18], player2[18], player3[18],
等
這裏是我的代碼到這一點:
import java.util.Scanner;
import java.io.*;
public class Golf
{
public static void main(String[] args) throws java.io.IOException
{
Scanner countScan = new Scanner(new File("golfscores.txt"));
Scanner file = new Scanner(new File("golfscores.txt"));
//------------------------------------------------------------
// Counting the number of players
//
// This takes the number of integers in the file, divides it by
// the 18 holes in the course and subtracts 1 for the par.
//
// I needed to count the players because it's a variable that
// can change depending on how many players are entered in the
// java program that creates a random scorecard.
//------------------------------------------------------------
int players = 0;
for (int temp = 0; countScan.hasNextInt(); players++)
temp = countScan.nextInt();
players = players/18-1;
//------------------------------------------------------------
//Creating necessary arrays
//------------------------------------------------------------
}
}
編輯:我必須對每個玩家的數組,我不允許使用的ArrayList。在這一點上,它看起來像我將使用一些數組中的數組所建議的評論。不知道這是一件事情(顯然我很小白)。
爲什麼不數組的數組? –
製作一個數組(矩陣)的陣列是想到的。 – Gendarme
看起來我可能會與數組陣列。不知道存在!謝謝。 – Kyle