import java.io.*;
import java.util.*;
public class A {
static String[] inf = null;
static String[] num = null;
static List<String[]> stu = new ArrayList<String[]>();
static double[] sc = new double[13];
public static void test6(){
try {
String filepath = "C:\\\\Users\\\\陈柏宇\\\\Desktop\\\\新建文件夹\\\\LP002.txt";
BufferedReader reader = new BufferedReader(new FileReader(filepath)); String temp = reader.readLine();
String[] arr = null;
int row = 0; while (temp != null) {
row++;
arr = temp.split(",");
if (row == 1) {
String course = arr[0];
String credit = arr[1];
inf = arr;
} if (row == 2) {
String number = arr[0];
num = arr;
} if (row > 2) {
String fname = arr[0];
String lname = arr[1];
String id = arr[2];
String grade = arr[3];
stu.add(arr);
}
//
temp = reader.readLine();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//Get the Maxim
public static double Max() {
String[] d = null;
for (int j = 0; j < stu.size(); j++) {
d = stu.get(j);
sc[j] = Double.valueOf(d[3]);
}
double max = sc[0];
//Get the max score
for (int j = 1; j <= num.length; j++) {
if (sc[j] > max)
max = sc[j];
}
return max;
}
public static double Min() {
String[] d = null;
for (int j = 0; j < stu.size(); j++) {
d = stu.get(j);
sc[j] = Double.valueOf(d[3]);
}
double min = sc[0];
//Get the max score
for (int j = 1; j <= num.length; j++) {
if (sc[j] < min)
min = sc[j];
}
return min;
}
//Get the average score
public static double Aver() {
String[] d = null;
for (int j = 0; j < stu.size(); j++) {
d = stu.get(j);
sc[j] = Double.valueOf(d[3]);
}
double all = 0;
double average = 0;
for(int a = 0; a < num.length; a++) {
all += sc[a];
average = all/sc.length;
}
return average;
}
public static void main(String[] argu) {
test6();
String[] arr = null;
System.out.println("Course:" + inf[0]);
System.out.println("Credit:" + inf[1]);
System.out.println("Number of the students:" + num[0] + "\n");
System.out.println("The highest score:" + Max());
System.out.println("The lowest score:" + Min());
System.out.println("The average score:" + Aver() + "\n");
System.out.println(" Name" + " \t\tID\t\t" + "Grade\t");
for (int i = 1; i < stu.size(); i++) {
arr = stu.get(i);
System.out.print(i + " " + arr[0] +"," + arr[1]);
System.out.print("\t" + arr[2]);
System.out.println("\t" + arr[3]);
}
}
}
double[] arr = new double[] { 66.38, 70.17, 67.32, 77.73, 53.00, 96.00, 35.33, 56.03, 64.02, 85.38, 87.93,
62.33 };double max = arr[0];
double min = arr[0];
double sum = 0;
double tmp;
for (int i = 1; i < arr.length; i++) {
tmp = arr[i];
sum += tmp;
if (tmp > max) {
max = tmp;
} else if (tmp < min) {
min = tmp;
}
}
double avg = sum / arr.length;
System.out.println("最大值:" + max + ", 最小值:" + min + ", 平均数:" + avg);运行结果:最大值:96.0, 最小值:35.33, 平均数:62.936666666666675
将“double sum = 0;”改成“double sum = arr[0];”,否则,第一个元素没有加到总和里面。
13
Tung,Grace,1109853A-I011-0022,80.00
Jing,Vito,1109853U-I011-0113,66.38
Zhong,Amy,11098537-I011-0355,70.17
Yu,Fiona,1109853U-I011-0056,67.32
Lam,Stephen,11098536-I011-3419,77.73
Jia,Wallace,1109853C-I011-3905,53.00
Rui,Theodore,1109853L-I011-3126,96.00
Cai,Terry,11098531-I011-0437,35.33
Gui,Kathie,11098539-I011-0087,56.03
Teng,Robert,11098532-I011-8718,64.02
Weng,Frank,11098537-I011-9244,85.38
Pun,Alice,11098538-I011-0174,87.93
Vong,Alex,1109853J-I011-3679,62.33
Number of the students:13The highest score:80.0
The lowest score:66.38
The average score:6.153846153846154 Name ID Grade
1 Jing,Vito 1109853U-I011-0113 66.38
2 Zhong,Amy 11098537-I011-0355 70.17
3 Yu,Fiona 1109853U-I011-0056 67.32
4 Lam,Stephen 11098536-I011-3419 77.73
BufferedReader reader = null;
try {
// String filepath = "C:\\\\Users\\\\陈柏宇\\\\Desktop\\\\新建文件夹\\\\LP002.txt";
String filepath = "LP002.txt";
reader = new BufferedReader(new FileReader(filepath)); String temp = reader.readLine();
String[] arr = null;
int row = 0; while (temp != null) {
row++;
arr = temp.split(",");
// 这里改写了判断的逻辑,可以在循环时减少判断次数,提高效率
if (row > 2) {
// 注释掉了没有使用过的局部变量,可以节约资源
// String fname = arr[0];
// String lname = arr[1];
// String id = arr[2];
// String grade = arr[3];
stu.add(arr);
} else if (row == 2) {
// String number = arr[0];
num = arr;
} else {
// String course = arr[0];
// String credit = arr[1];
inf = arr;
}
temp = reader.readLine();
} } catch (Exception e) {
e.printStackTrace();
} finally {
// 显式的关闭使用过的资源是一个好习惯
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
我不知道你要实现什么效果,总之,觉得你的代码逻辑有点乱,效率有点低。
if (sc[j] < min)
min = sc[j];
}
return min;
}