gpt4 book ai didi

java - 如何查找来自 csv 文件的数组列表的平均分(JAVA)

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

我有一个 csv 文件,其中包含学生 ID、姓氏、名字、分数、电话号码等列表。我已将它们组织到一个数组列表中,以便当您调用 stu[100].mark 时它将在 csv 文件列表中找到第 100 个学生的分数。有学生1000人。我需要计算所有学生的总平均分(每个学生只有一个分数)我把它放在一个循环中,但它只是打印出他们的分数。如果您需要更多详细信息,我深表歉意。

这是代码:

公开课StudentRecs{

public static String user;
public int StuRec;
public static int numstu;
public static double average;
//public static StuRec[] stu;
static StuRec[] stu = new StuRec[1000];

public static void main(String[] args) throws IOException {
for (int i = 0; i < 1000; i++) {
stu[i] = new StuRec();
}
StuRec stu = new StuRec();
readFile(user);
menu();
}
public static String filename;
Scanner reader = new Scanner(filename);

public static Boolean readFile(String filename) throws IOException { //Constructor for filename
try {
Scanner userInput = new Scanner(System.in);

System.out.println("Type R To Read a File or Type Default for the default file");
user = userInput.nextLine();
if (user.equalsIgnoreCase("r")) {
user = userInput.nextLine();
}
filename = user;
if (user.equalsIgnoreCase("default")) {
filename = "newreg2.csv";
}


Scanner input = new Scanner(new FileReader(filename));
while (input.hasNext()) {
in(input.nextLine());
numstu++;
}
input.close();
return true;

} catch (IOException e) {
System.err.println(e.getMessage());
}
return false;
}


public static void in(String reader) {
String splitter[];
splitter = reader.split(",");
stu[numstu] = new StuRec();
stu[numstu].studentID = splitter[0];
stu[numstu].lastName = splitter[1];
stu[numstu].firstName = splitter[2];
stu[numstu].phoneNumber = splitter[3];
stu[numstu].courseCode = splitter[4];
stu[numstu].periodNumber = Integer.parseInt(splitter[5]); // parseInt turns a string of digits into an integer
stu[numstu].mark = Integer.parseInt(splitter[6]);
}

public static boolean menu() {
int total = 0;
String choice;
Scanner userInput = new Scanner(System.in);
System.out.println("=============================================");
System.out.println("Type R To Read Another File");
System.out.println("Type L To Print all File Records");
System.out.println("Type AA To Print The Average Of All The Marks");
choice = userInput.nextLine();
for (int i = 0; i < numstu; i++) {
if (choice.equalsIgnoreCase("L")) {
System.out.println(stu[i].lastName + ", " + stu[i].firstName + ", " + stu[i].studentID + ", " + stu[i].phoneNumber + ", " + stu[i].courseCode + ", " + stu[i].periodNumber + ", " + stu[i].mark);
}else if (choice.equalsIgnoreCase("R")){

} else if (choice.equalsIgnoreCase("AA")) {
total = total + stu[i].mark;

} else {
System.err.println("Unknown Key Try Again...");

}

average = total / 1000; // compute the average.
System.out.println(average);

} return menu();

}}

最佳答案

您的平均例程计算不正确。它只是取第 n 个标记并将其添加到自身,除以 1000。由于您使用的是整数,因此它只会向下舍入,有效地为您提供每次迭代的平均值第 n 个标记 + 0,从而为您留下循环完成的最后一个标记。

您需要继续添加平均值,完成后除以 1000 即可得到值。

public static boolean menu() {
String choice;
Scanner userInput = new Scanner(System.in);
System.out.println("=============================================");
System.out.println("Type R To Read Another File");
System.out.println("Type L To Print all File Records");
System.out.println("Type AA To Print The Average Of All The Marks");
choice = userInput.nextLine();

for (int i = 0; i < numstu; i++) {
if (choice.equalsIgnoreCase("L")) {
System.out.println(stu[i].lastName + ", " + stu[i].firstName + ", " + stu[i].studentID + ", " + stu[i].phoneNumber + ", " + stu[i].courseCode + ", " + stu[i].periodNumber + ", " + stu[i].mark);
}else if (choice.equalsIgnoreCase("R")){

} else if (choice.equalsIgnoreCase("AA")) {
average += stu[i].mark; // keep adding to average

} else {
System.err.println("Unknown Key Try Again...");

}
}
// divide by zero protection
if ( numstu > 0 ) {
average = average/numstu; // compute the average. Always use the size in terms of a variable whenever possible.
System.out.println(average); // as noted below, if this is an integer value, < #of students computations will eval to 0.
// might be better to use double
}
else {
System.out.println("Oops! No students! :(");
}
return menu();
}

请注意,您的菜单效率有点低,但手头的问题应该可以解决。

关于java - 如何查找来自 csv 文件的数组列表的平均分(JAVA),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20085118/

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