java輸入學生資訊,按總分成績由高到低排列,輸出到檔案中
阿新 • • 發佈:2018-12-30
<span style="font-family: Arial, Helvetica, sans-serif;">主函式</span>
學生類import java.io.BufferedOutputStream; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.Writer; import java.util.Comparator; import java.util.Iterator; import java.util.Scanner; import java.util.Set; import java.util.TreeSet; public class TestStudent { <span style="white-space:pre"> </span>public static void main(String[] args) { <span style="white-space:pre"> </span>//String name=""; <span style="white-space:pre"> </span>int i=0; <span style="white-space:pre"> </span>Set<Student> set= new TreeSet<Student>(new Comparator<Student>(){ <span style="white-space:pre"> </span>@Override <span style="white-space:pre"> </span>public int compare(Student o1, Student o2) { <span style="white-space:pre"> </span>// TODO Auto-generated method stub <span style="white-space:pre"> </span>if(o1.getScore()<o2.getScore()){ <span style="white-space:pre"> </span>return 1; <span style="white-space:pre"> </span>}else if(o1.getScore()>o2.getScore()){ <span style="white-space:pre"> </span>return -1; <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>return 0; <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>}); <span style="white-space:pre"> </span>while(true) { <span style="white-space:pre"> </span>System.out.println("請輸入學生的姓名"); <span style="white-space:pre"> </span>String sname= new Scanner(System.in).next(); <span style="white-space:pre"> </span>System.out.println("請輸入學生的語文"); <span style="white-space:pre"> </span>int s1= new Scanner(System.in).nextInt(); <span style="white-space:pre"> </span>System.out.println("請輸入學生的數學"); <span style="white-space:pre"> </span>int s2= new Scanner(System.in).nextInt(); <span style="white-space:pre"> </span>System.out.println("請輸入學生的英語"); <span style="white-space:pre"> </span>int s3= new Scanner(System.in).nextInt(); <span style="white-space:pre"> </span>int s4=s1+s2+s3; <span style="white-space:pre"> </span>Student ss1=new Student(sname,s1,s2,s3,s4); <span style="white-space:pre"> </span>set.add(ss1); <span style="white-space:pre"> </span>System.out.println("是否繼續進行新增"+1+"就是退出"); <span style="white-space:pre"> </span>int out= new Scanner(System.in).nextInt(); <span style="white-space:pre"> </span>i++; <span style="white-space:pre"> </span>if(i==5||out==1){ <span style="white-space:pre"> </span>break; <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>Iterator<Student> iter=set.iterator(); <span style="white-space:pre"> </span>while(iter.hasNext()){ <span style="white-space:pre"> </span>Student sh=iter.next(); <span style="white-space:pre"> </span>//System.out.println(sh); <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>File f1= new File("D:\\testio\\a\\s.txt"); <span style="white-space:pre"> </span>TestStudent.cunru(set, f1); <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>public static void cunru(Set<Student> set,File f1){ <span style="white-space:pre"> </span>if(!f1.exists()){ <span style="white-space:pre"> </span>f1.getParentFile().mkdirs(); <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>Writer w1= null; <span style="white-space:pre"> </span>BufferedWriter b1=null; <span style="white-space:pre"> </span>try { <span style="white-space:pre"> </span>w1= new FileWriter(f1); <span style="white-space:pre"> </span>b1=new BufferedWriter(w1); <span style="white-space:pre"> </span>Iterator<Student> iter=set.iterator(); <span style="white-space:pre"> </span>while(iter.hasNext()){ <span style="white-space:pre"> </span>Student sh=iter.next(); <span style="white-space:pre"> </span>System.out.println(sh.toString()); <span style="white-space:pre"> </span>b1.write(sh.toString()); <span style="white-space:pre"> </span>b1.newLine(); <span style="white-space:pre"> </span>//System.out.println(sh); <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>b1.flush(); <span style="white-space:pre"> </span>} catch (IOException e) { <span style="white-space:pre"> </span>// TODO Auto-generated catch block <span style="white-space:pre"> </span>e.printStackTrace(); <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>} }
<pre name="code" class="java"><span style="font-family: Arial, Helvetica, sans-serif;">public class Student {</span>
private String name; private int yuwen; private int shuxue; private int yingyu; private int score; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getYuwen() { return yuwen; } public void setYuwen(int yuwen) { this.yuwen = yuwen; } public int getShuxue() { return shuxue; } public void setShuxue(int shuxue) { this.shuxue = shuxue; } public int getYingyu() { return yingyu; } public void setYingyu(int yingyu) { this.yingyu = yingyu; } public int getScore() { return score; } public void setScore(int score) { this.score = score; } @Override public String toString() { return "Student [name=" + name + ", yuwen=" + yuwen + ", shuxue=" + shuxue + ", yingyu=" + yingyu + ", score=" + score + "]"; } /** * @param name * @param yuwen * @param shuxue * @param yingyu */ public Student(String name, int yuwen, int shuxue, int yingyu) { this.name = name; this.yuwen = yuwen; this.shuxue = shuxue; this.yingyu = yingyu; } /** * @param name * @param yuwen * @param shuxue * @param yingyu * @param score */ public Student(String name, int yuwen, int shuxue, int yingyu, int score) { super(); this.name = name; this.yuwen = yuwen; this.shuxue = shuxue; this.yingyu = yingyu; this.score = score; } }
檔案輸出結果: