有五个学生,每个学生有3门课(数学、语文、英语)的成绩,写一个程序接收从键盘输入学生的信息,
* 输入格式为:name,30,30,30(姓名,三门课成绩),然后把输入的学生信息按总分从高到低的顺序
* 写入到一个名称"stu.txt"文 件中。要求:stu.txt文件的格式要比较直观, 打开这个文件,就可以很清楚的看到学生的信息。
package c;
public class Student {
private String name;
private int math;
private int chinese;
private int englise;
public Student() {
// TODO Auto-generated constructor stub
}
public Student(String name, int math, int chinese, int englise) {
super();
this.name = name;
this.math = math;
this.chinese = chinese;
this.englise = englise;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getMath() {
return math;
}
public void setMath(int math) {
this.math = math;
}
public int getChinese() {
return chinese;
}
public void setChinese(int chinese) {
this.chinese = chinese;
}
public int getEnglise() {
return englise;
}
public void setEnglise(int englise) {
this.englise = englise;
}
}
package c;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
public class StudentTest {
public static void main(String[] args) throws Exception {
/*
* 4、 有五个学生,每个学生有3门课(数学、语文、英语)的成绩,写一个程序接收从键盘输入学生的信息,
* 输入格式为:name,30,30,30(姓名,三门课成绩),然后把输入的学生信息按总分从高到低的顺序
* 写入到一个名称"stu.txt"文 件中。要求:stu.txt文件的格式要比较直观, 打开这个文件,就可以很清楚的看到学生的信息。
* */
System.out.println("please input imfermation of student:(name math chinese english)");
Student s1 = new Student();
Student s2 = new Student();
Student s3 = new Student();
Student s4 = new Student();
Student s5 = new Student();
ArrayList<Student> al = new ArrayList<Student>();
al.add(s1);
al.add(s2);
al.add(s3);
al.add(s4);
al.add(s5);
Iterator<Student> iter = al.iterator();
int[] sarr = new int[5];
int count = 0;
Scanner sc = new Scanner(System.in);
while (iter.hasNext()) {
System.out.println("这是第:"+(count+1)+"个学生的信息");
Student s = iter.next();
String name = sc.next();
s.setName(name);
s.setMath(sc.nextInt());
s.setChinese(sc.nextInt());
s.setEnglise(sc.nextInt());
sarr[count++] = s.getMath()+s.getChinese()+s.getEnglise();
}
sc.close();
fun(sarr);
File f = new File("Student.txt");
FileOutputStream fos = new FileOutputStream(f);
OutputStreamWriter osw = new OutputStreamWriter(fos);
int a = 4;
for (int i = 0; i < sarr.length; i++) {//用于控制循环次数
for (int j = 0; j < sarr.length; j++) {//用于从大到小的输出
if(sarr[j] == a){
//xie
Student s = al.get(j);
osw.write(s.getName() + "\t");
osw.write("math:"+s.getMath() );
osw.write("\t chinese:" + s.getChinese());
osw.write("\tenglish:" + s.getEnglise() +"\n");
a--;
break;
}
}
}
osw.close();
fos.close();
System.out.println(sarr[1]);
}
public static void fun(int[] arr){//bb
int[] b = new int[arr.length];
for (int i = 0; i < arr.length; i++) {
int count = 0;
for (int j = 0; j < arr.length; j++) {
if(i==j)continue;
if(arr[i]>arr[j])count++;
}
b[i] = count;
}
for(int k = 0;k<b.length;k++){
arr[k] = b[k];
}
}
}
|
|