gpt4 book ai didi

java - 将字符串文件作为整数读取到数组中?

转载 作者:行者123 更新时间:2023-12-01 09:17:23 26 4
gpt4 key购买 nike

我需要编写一个程序,该程序接受一个包含高尔夫球手姓名、年龄和得分信息的输入文件 (practice.txt)。然后,该文件应将分数与特定年龄组的标准分数进行比较。例如:Manny 14 岁,因此他遵循 12-15 岁年龄段的标准分数。曼尼在第一洞打出5杆,因此他保帕了。我无法理解如何比较文件和数组?任何能让我上路的代码或想法都会很棒。

输入文件practice.txt:

杰伊 57 4 3 2 3 5 3 2 3 4

格洛丽亚 39 4 4 3 4 3 4 3 3 5

曼尼14 5 6 4 6 5 6 4 4 6

乔3 9 8 8 7 6 6 7 5 7

标准杆分数图表:

                           HOLES
AGE 1 2 3 4 5 6 7 8 9

4 and under 8 8 9 7 5 7 8 5 8
5 – 7 7 7 8 6 5 6 7 5 6
8 – 11 6 5 6 5 4 5 5 4 5
12 – 15 5 4 4 4 3 4 3 3 4
16 and over 4 3 3 3 2 3 2 3 3

我拥有的:

import java.io.File;
import java.util.Scanner;
import java.util.List;
import java.io.FileNotFoundException;
import java.util.ArrayList;

public class imTryingHere {
public static void main (String[] args) throws FileNotFoundException
{

int [][] ageGroups =
{
{4},
{7},
{11},
{15},
{100},
};

int[][] holePars=
{
{8,8,9,7,5,7,8,5,8},
{7,7,8,6,5,6,7,5,6},
{6,5,6,5,4,5,5,4,5},
{5,4,4,4,3,4,3,3,4},
{4,3,3,3,2,3,2,3,3},
};


}
}

最佳答案

试试这个。

// Put age and par scores into Map
static Map<Integer, List<Integer>> ageParsList = new LinkedHashMap<Integer, List<Integer>>() {
{put(4, new LinkedList<Integer>(Arrays.asList(8, 8, 9, 7, 5, 7, 8, 5, 8)));}
{put(7, new LinkedList<Integer>(Arrays.asList(7, 7, 8, 6, 5, 6, 7, 5, 6)));}
{put(11, new LinkedList<Integer>(Arrays.asList(6, 5, 6, 5, 4, 5, 5, 4, 5)));}
{put(15, new LinkedList<Integer>(Arrays.asList(5, 4, 4, 4, 3, 4, 3, 3, 4)));}
{put(-1, new LinkedList<Integer>(Arrays.asList(4, 3, 3, 3, 2, 3, 2, 3, 3)));}
};

public static List<Integer> getParScoresList(int age) {

int targetAge = -1;
for (Integer key : ageParsList.keySet()) {
if (age <= key) {
targetAge = key;
break;
}
}

return ageParsList.get(targetAge);
}

public static class Player {
String name;
int age;
List<Integer> scoreList;

public Player(String name, int age, List<Integer> scoreList) {
this.name = name;
this.age = age;
this.scoreList = scoreList;
}
}

private static List<Player> getPlayerScore() {
Scanner scan = null;
try{
File file = new File(_path_to_file_);
scan = new Scanner(file);

List<Player> playerList = new ArrayList<Player>();

while (scan.hasNext()) {
String name = scan.next();
int age = scan.nextInt();
List<Integer> scoreList = new LinkedList<Integer>();
for (int i = 0; i < 9; i++) {
scoreList.add(scan.nextInt());
}
scan.nextLine(); // last line must have newline character.

Player player = new Player(name, age, scoreList);
playerList.add(player);
}

return playerList;
} catch(FileNotFoundException e){
System.out.println(e);
return null;
} finally {
if (scan != null) {
scan.close();
}
}
}

public static void main (String[] args) {
List<Player> playerList = getPlayerScore();

for (Player player : playerList) {
List<Integer> parScoresList = getParScoresList(player.age);

for (int i = 0; i < 9; i++) {
int score = player.scoreList.get(i);
int par = parScores.get(i);

// Compare player's score and par score here.
}
}
}

关于java - 将字符串文件作为整数读取到数组中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40444992/

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