我必须编写一个程序来保存一个有 10 名球员的篮球队的统计数据。统计数据是出手次数、命中率;罚球尝试,命中率;进攻篮板和防守篮板;助攻;失误和总分。他们需要放入并行数组中。我正在尝试修改高尔夫记分卡程序,但在找出数组时遇到问题。统计信息将使用以下格式的 txt 文件输入:
10 //这是玩家的数量 1 //这是玩家1 20 //射门尝试 15 //投篮命中 4 //罚球尝试 3 //罚球命中 2 //关闭反弹 2 //def反弹 5 //助攻 2 //营业额 18 //总分 2 //10个玩家以此类推
如果需要,我可以修改 txt 文件
以下是我迄今为止修改高尔夫记分卡的尝试:
import java.util.Scanner;
import java.io.*;
public class BasketballScoreCard {
private int[] player;
private int[][] scores;
/////////////////////////////////////////////////////////////////
// Instantiate and read golf scores from a text file
public BasketballScoreCard(String fileName) throws IOException {
// Open the file
Scanner fileReader = new Scanner(new File(fileName));
// Read the number of days
int numPlayer = fileReader.nextInt();
// Instantiate dates and dailyScores
player = new int[numPlayer];
scores = new int[numPlayer][18];
// Read the scores for each day
// Date (yyyymmdd), followed by 18 scores
for (int i = 0; i < numPlayer; i++){
player[i] = fileReader.nextInt() ;
for (int j = 0; j < 18; j++)
scores[i][j] = fileReader.nextInt();
}
// Close the file
fileReader.close();
}
/////////////////////////////////////////////////////////////////
// Return a string with one line per day.
// Each line consists of a date and 18 scores
public String toString(){
String str = "";
for (int i = 0; i < player.length; i++){
str += "Player: " + player[i] + " \nScores:";
for (int j = 0; j < 18; j++)
str += " " + scores[i][j];
str += "\n";
}
return str;
}
/////////////////////////////////////////////////////////////////
// Return a string with two lines.
// The first line contains the date and scores for the best day
// The second line contains the date and scores for the worst day
public String highLowDays(){
// Assume that the first day is the best and worst
int indexLow = 0;
int indexHigh = 0;
int lowTotal = dayTotal(0);
int highTotal = dayTotal(0);
// Now consider the remaining days
for (int i = 1; i < player.length; i++){
int todayTotal = dayTotal(i);
if (todayTotal < lowTotal){
indexLow = i;
lowTotal = todayTotal;
}else if (todayTotal > highTotal){
indexHigh = i;
highTotal = todayTotal;
}
}
// Format the return string
String str = "";
str += "The best day: " + player[indexLow] + " score: " + lowTotal + "\n";
str += "The worst day: " + player[indexHigh] + " score: " + highTotal + "\n";
return str;
}
// Return the total for the indicated day
private int dayTotal (int i){
int total = 0;
for (int j = 0 ; j < 18; j++)
total += scores[i][j];
return total;
}
/////////////////////////////////////////////////////////////////
// Return a string with two lines.
// The first line indicates the hole with the lowest average
// The second line indicates the hole with the highest average
public String bestWorstHoles(){
String str = "";
return str;
}
// Return the average for the indicated hole
private double holeAverage (int j){
int numPlayer = player.length;
double average = 0;
for (int i = 0 ; i < numPlayer; i++)
average += scores[i][j];
return average / numPlayer;
}
}
我似乎不明白是否需要为每个统计数据添加一个数组或为每个统计数据使用一个数组。如果我需要为每个统计数据使用一个数组,那么如何将文本文件中的信息引入每个单独的数组?