gpt4 book ai didi

java - 创建学生文件,查看平均成绩高于5的学生信息以及每个学生的平均成绩

转载 作者:行者123 更新时间:2023-12-01 17:47:39 24 4
gpt4 key购买 nike

//Create the file Alumns.txt where we have a group of students with their last names, names and average grade, separed by “,” and view the information from the students that have more average grade from 5 and the average grade from each student.

//We have the file Alumns.txt with the content:

//雷莫·拉米雷斯、诺埃利亚、6、希门尼斯·桑托斯、卡洛斯、4、佩雷斯·戈麦斯、费尔南多、7、汉密尔顿·布莱恩、泰勒、3、古铁雷斯·门多萨、何塞、8、加西亚·雷耶斯、玛丽亚、6、马尔坎特·门德斯、卢娜,3 岁,德尔加多·门德斯,安吉尔,10 岁。

public class Main {

public static void main(String[] args) {


File fichero = null;
FileReader fr = null;
Scanner sc = null;
double [] notas = new double [6];

文件 Alumnos.txt

        fichero = new File ("Alumnos.txt");
try {
BufferedReader br = new BufferedReader(fr);
String texto2 = br.readLine();
fr = new FileReader(fichero);
sc = new Scanner(fichero);
String texto = sc.nextLine();
StringTokenizer tokens = new StringTokenizer(texto2, ",");

//我尝试使用 Scanner 和 BufferedReader,“notas”(数字)各为 3 个标记。

            for (int i = 0; i < notas.length; i++) {
tokens.nextToken();
tokens.nextToken();
notas[i] = (double)Double.parseDouble(tokens.nextToken());
System.out.println("Nota: "+notas[i]);
if (notas [i]>5) {
System.out.println("Pass the grade 5: "+tokens.nextToken());
}
}
//I catch the exceptions here:

} catch (FileNotFoundException e) {

e.printStackTrace();
} catch (IOException e) {

e.printStackTrace();
}

}

}

最佳答案

假设我们有这个文件grades.csv

Remo Ramirez,Noelia,6
Jimenez Santos,Carlos,4
Perez Gomez,Fernando,7
Hamilton Bryan,Taylor,3
Gutierrez Mendoza,Jose,8
Garcia Reyes,Maria,6
Marchante Mendez,Luna,3
Delgado Mendez,Angel,10

我们可以编写这个方法来解析它并仅打印平均成绩高于 5 的学生。

public static void printGradesBetterThanFive() throws IOException {

//Create a file object, path to the file might differ in your case.
File grades = new File("grades.csv");
if (grades.isFile()) {

//Create a BufferedReader, taking in a FileReader with our File as an argument
BufferedReader csvReader = new BufferedReader(new FileReader(grades));

//Parse through each line in the file until we reach the end of it
String line;
while ((line = csvReader.readLine()) != null) {

//Create an array containing values from each line, separated by commas.
String[] lineData = line.split(",");

//If the last value (the grade) is more than 5, print it.
if (Integer.parseInt(lineData[2]) > 5) {
System.out.println(Arrays.toString(lineData));
}
}
csvReader.close();
}
}

您的问题有点模糊,但希望这对您有所帮助。

关于java - 创建学生文件,查看平均成绩高于5的学生信息以及每个学生的平均成绩,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60838368/

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