BJFU Java 期末考试题(回忆版)

logo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Student{
String all;
String onescore;
void getone(){
for(int i=0;i<all.length();i++){
if(all.charAt(i)==' '){
onescore=all.substring(i, all.length());
break;
}
}
}
}
class wln extends JFrame implements ActionListener{
JLabel jlb1=new JLabel("请输入文件名及路径:");
JTextField jtf=new JTextField(20);
JButton jbt1=new JButton("打开文件"),
jbt2=new JButton("保存文件"),
jbt3=new JButton("第一个成绩按升序排序");
JTextArea jta1=new JTextArea(),
jta2=new JTextArea();
JScrollPane jsp1=new JScrollPane(jta1),
jsp2=new JScrollPane(jta2);
JPanel jpl=new JPanel(),
jpl1=new JPanel(),
jpl2=new JPanel(),
jpl3=new JPanel(),
jpl4=new JPanel();
public wln() {
jpl1.add(jlb1);
jpl1.add(jtf);
jpl2.setLayout(new GridLayout(1, 3,0,0));
jpl2.add(jbt1);
jbt1.addActionListener(this);
jpl2.add(jbt2);
jbt2.addActionListener(this);
jpl2.add(jbt3);
jbt3.addActionListener(this);
jpl3.setLayout(new GridLayout(2, 1));
jpl3.add(jpl1);
jpl3.add(jpl2);
add(jpl3,BorderLayout.NORTH);
jpl4.setLayout(new GridLayout(1, 2));
jpl4.add(jsp1);
jpl4.add(jsp2);
add(jpl4, BorderLayout.CENTER);
setTitle("学生信息");
setBounds(0,0,666,666);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
Object obj=e.getSource();
if(obj==jbt1){
try{
String p=jtf.getText();
FileReader fr=new FileReader(p);
BufferedReader bfr=new BufferedReader(fr);
String s=bfr.readLine();
if(!jta1.getText().equals("")){
jta1.append("\n");
}
while(s!=null){
jta1.append(s+"\n");
s=bfr.readLine();
}
bfr.close();
fr.close();
}catch (IOException e1) {
System.out.println("文件打开失败");
}
}else if(obj==jbt2){
try{
String p=jtf.getText();
FileWriter fw=new FileWriter(p);
BufferedWriter bfw=new BufferedWriter(fw);
bfw.write(jta1.getText());
jta2.setText("保存成功");
bfw.flush();
bfw.close();
fw.close();
}catch (IOException e1) {
System.out.println("文件保存失败");
}

}else if(obj==jbt3){
try{
String p=jtf.getText();
FileReader fr=new FileReader(p);
BufferedReader bfr=new BufferedReader(fr);
String s=bfr.readLine();
Student []stu=new Student[100];
int n=0;
while(s!=null){
stu[n]=new Student();
stu[n].all=new String();
stu[n].onescore=new String();
stu[n].all=s;
stu[n++].getone();
s=bfr.readLine();
}
for(int i=0;i<n-1;i++){
for(int j=i+1;j<n;j++){
if(stu[i].onescore.compareTo(stu[j].onescore)>0){
Student temp=stu[i];
stu[i]=stu[j];
stu[j]=temp;
}
}
}
if(!jta2.getText().equals("")){
jta2.append("\n");
}
for(int i=0;i<n;i++){
jta2.append(stu[i].all+"\n");
}
bfr.close();
fr.close();
}catch (IOException e1) {
System.out.println("文件打开失败");
}
}

}
}
public class main {
public static void main(String []args){
new wln();
}
}