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]);
}
}
}

解决方案 »

  1.   

    也就是说,你需要的在double数组中求最大、最小、平均数,不需要考虑从外部文件读取的问题,是吗?
      

  2.   


    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
      

  3.   

    上面有错误
    将“double sum = 0;”改成“double sum = arr[0];”,否则,第一个元素没有加到总和里面。
      

  4.   

    我这个应该不是方法的问题,我在complier的时候他给我报错是我超出了储存的范围,所以我不知道错在哪里了,
      

  5.   

    把你的LP002.txt内容发出来,我调试一下看看
      

  6.   

    LP002,4
    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
      

  7.   

    我调试了,没有问题啊,下面是输出的部分内容Credit:4
    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
      

  8.   

    while里面的那3个if,可以用if-elseif结构改写,先判断row>2,其他两个顺序无所谓,这样在循环时可以减少判断次数,效率会好一些。
      

  9.   

    下面是我改写过的test6()方法public static void test6() {
    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();
    }
    }
    }
    }
      

  10.   

    Max()和Min()方法也有可商榷的地方,我感觉,中test6()中,一遍读文本文件,一边把数据处理了就好了,最好写一个实体类Student,把每行数据组织成一个实例,然后用一个List保存,这样比较符合Java面向对象的编程思路。
    我不知道你要实现什么效果,总之,觉得你的代码逻辑有点乱,效率有点低。
      

  11.   

    我这个应该不是方法的问题,我在complier的时候他给我报错是我超出了储存的范围,所以我不知道错在哪里了,你下面一段循环有问题 sc最大下标为12,你这里1-13肯定出问题了。for (int j = 1; j <= num.length; j++) {
    if (sc[j] < min)
    min = sc[j];
    }
    return min;
    }