📄 threegrade.java
字号:
import java.io.*;
import java.util.Scanner;
public class ThreeGrade {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int n;
float MaxScore=0f,MinScore=0f,AvgScore=0f;
Scanner scanner = new Scanner(System.in);
System.out.println("please input n");
n=scanner.nextInt();
String[] name = new String[n];
float[] score = new float[n];
System.out.println("please input the name and grade of every student:");
for(int i=0;i<n;i++)
{ // student[i] = new String();
name[i] = scanner.next();
score[i] = scanner.nextFloat();
}
RandomAccessFile file1=null;
RandomAccessFile file2=null;
/**
* write the name and grade into the file1
*/
try {
file1 = new RandomAccessFile("f:\\1.dat","rw");
for(int i = 0;i<n;i++)
{
file1.writeUTF(name[i]);
file1.writeFloat(score[i]);
}
} catch (FileNotFoundException e) {
// TODO: handle exception
e.printStackTrace();
}catch (IOException e1) {
// TODO: handle exception
e1.printStackTrace();
}finally{
if(file1 != null){
try {
file1.close();
} catch (Exception e2) {
// TODO: handle exception
e2.printStackTrace();
}
}
}
try {
file2=new RandomAccessFile("f:\\2.dat","rw");
file1 = new RandomAccessFile("f:\\1.dat","r");
float[] score2 = new float[n];
String[] name2 = new String[n];
int i=0;
name2[i] = file1.readUTF();
score2[i]= file1.readFloat();
MaxScore = MinScore = score2[i];
AvgScore = AvgScore+score2[i];
for( i=1;i<n;i++){
name2[i] = file1.readUTF();
score2[i]= file1.readFloat();
AvgScore = AvgScore+score2[i];
if(i!=1){
if(score2[i]>MaxScore)MaxScore=score[i];
if(score2[i]<MinScore)MinScore=score[i];
}
}
System.out.println("the max score is "+MaxScore);
System.out.println("the min score is "+MinScore);
System.out.println("the avager score is "+AvgScore/n);
file2.writeFloat(MaxScore);
file2.writeFloat(MinScore);
file2.writeFloat(AvgScore);
} catch (FileNotFoundException e) {
// TODO: handle exception
e.printStackTrace();
}catch (IOException e1) {
// TODO: handle exception
e1.printStackTrace();
}finally{
if(file2 != null){
try {
file2.close();
}
catch (Exception e2) {
// TODO: handle exception
e2.printStackTrace();
}
}
if(file1 != null){
try {
file1.close();
}
catch (Exception e2) {
// TODO: handle exception
e2.printStackTrace();
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -