There are five students, each student has 3 courses (Chinese, maths, English) scores, * Write a program to receive information from the keyboard input students, the input format is: name,30,30,30 (name, three class results)

Source: Internet
Author: User

/** 3, there are five students, each student has 3 courses (Chinese, maths, English) scores, * Write a program to receive information from the keyboard input students, the input format is: name,30,30,30 (name, three class results), * The input student information is then written to a file named "Stu.txt" in the order of the total score from highest to lowest. * Requirements: Stu.txt file format to be more intuitive, open this file, you can clearly see the student's information **/ImportJava.io.*;ImportJava.util.*; Public classTest3 { Public Static voidMain (string[] args)throwsexception{//number of incoming studentsStudent (5); }    //Keyboard entry students, save to Stu.txt method     Public Static voidStudent (intNumberthrowsexception{//Create scanner object accept input from consoleScanner in=NewScanner (system.in); //because students may appear the same name and total score, in order to ensure that students do not lose, set up a list containerList<student> list=NewArraylist<>(); //cycle into the set of students, arrange the order .SYSTEM.OUT.PRINTLN ("Input format: Name, language, Math, English (, number is lowercase state)");  for(intx=1;x<=number;x++) {System.out.println ("Please enter the information for" +x+ "student"); String[] s=in.nextline (). Split (","); intA=integer.parseint (s[1]); intB=integer.parseint (s[2]); intC=integer.parseint (s[3]); Student Stu=NewStudent (s[0],a,b,c);        List.add (Stu); }        //sorting from high to low with comparatorsCollections.sort (list,NewStucompare ()); //define output stream output to Stu.txtBufferedWriter bufw=NewBufferedWriter (NewFileWriter ("Stu.txt"));  for(Student stu:list) {bufw.write (Stu.getstu ());            Bufw.newline ();        Bufw.flush (); }        //Close ResourceIn.close ();    Bufw.close (); }}//Student Objectsclassstudent{PrivateString name; Private intA, B, c,sum; //Student object establishment need to pass in name and 3 course gradeStudent (String name,intAintBintc) {         This. name=name;  This. a=A;  This. b=b;  This. c=C; Sum=a+b+C; }     Public intgetsum () {returnsum; }    //output A string that conforms to the format     PublicString Getstu () {returnname+ "Mathematics:" +a+ "Chinese:" +b+ "English:" +c+ "score:" +sum; }}//define comparators, sorted by total scoreclassStucompareImplementsComparator<student>{     Public intCompare (Student s1,student s2) {intA=NewInteger (S2.getsum ()). CompareTo (S1.getsum ()); returnA; }}

There are five students, each student has 3 courses (Chinese, maths, English) scores, * Write a program to receive information from the keyboard input students, the input format is: name,30,30,30 (name, three class results)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.