62,628
社区成员
发帖
与我相关
我的任务
分享
public static void main(String[] args) {
int sum = 0, average = 0;
int[] a = new int[5];
try {
OutputStream grade = new FileOutputStream("D:/data.txt");
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(grade));
for (int i = 0; i < 5; i++) {
int temp = (int) (Math.random() * 100) + 20;
bw.write("" + temp + "");
bw.newLine();
}
bw.flush();
bw.close();
FileInputStream gradeIn = new FileInputStream("D:/data.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(gradeIn));
for (int j = 0; j < 5; j++) {
a[j]=Integer.parseInt(br.readLine());
sum +=a[j];
}
average = sum / 5;
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (a[i] > a[j]) {
int t = a[j];
a[j] = a[i];
a[i] = t;
}
}
}
br.close();
} catch (Exception e) {
System.out.println("发生异常" + e);
e.printStackTrace();
}
System.out.println("这5个学生的平均成绩为: " + average);
System.out.print("这5个学生的成绩为: ");
for (int i = 0; i < 5; i++)
System.out.print(a[i] + " ");
}
这5个学生的平均成绩为: 95
这5个学生的成绩为: 117 111 105 92 51
public static void main(String[] args) {
int sum = 0, average = 0;
int[] a = new int[5];
try {
FileOutputStream grade = new FileOutputStream("d:/data.txt");
DataOutputStream dout = new DataOutputStream(grade);
for (int i = 0; i < 5; i++){
int temp = (int) (Math.random() * 100) + 20;
dout.writeInt(temp);
}
dout.close();
FileInputStream gradeIn = new FileInputStream("d:/data.txt");
DataInputStream fin = new DataInputStream(gradeIn);
for (int i = 0; i < 5; i++){
a[i] = fin.readInt();
sum += a[i];
}
average = sum / 5;
for (int i = 0; i < 5; i++)
for (int j = 0; j < 5; j++) {
if (a[i] > a[j]) {
int t = a[j];
a[j] = a[i];
a[i] = t;
}
}
fin.close();
} catch (Exception e) {
System.out.println("发生异常" + e);
e.printStackTrace();
}
System.out.println("这5个学生的平均成绩为: " + average);
System.out.print("这5个学生的成绩为: ");
for (int i = 0; i < 5; i++)
System.out.print(a[i] + " ");
}