Current location - Training Enrollment Network - Mathematics courses - Java mathematical sorting problem
Java mathematical sorting problem
Just written, there may be some loopholes.

Import? Java . util . *;

Import? Java . util . scanner;

Public? Class? Bd05? {

Public? Static electricity Final? Scanner? s? =? New? Scanner (system. in);

Public? Static electricity Invalid? main(String[]? args)? {

ArrayList & lt student & gt? Al. =? New? ArrayList & lt student & gt ();

Home (al);

}

Public? Static electricity Invalid? Home page (list & lt students & gt? List) {

while(true){

System.out.println(" 1。 Enter student information? 2. Inquire about student information? 3. Delete student information? 4. Exit \ nInput number selection: ");

int? num? =? s . nextint();

Switches (Qty)

Case? 1:

InputInfo (list);

Break;

Case? 2:

FindInfo (list);

Break;

Case? 3:

Delete (list);

Break;

Case? 4:

system . exit(0);

}

}

}

Public? Static electricity Invalid? findInfo(List & lt; Student & gt? List){// Query information

System.out.println(" 1。 Ascending order? 2. down 3. Return \ Enter a number to select the query method (total score of two subjects): ";

int? num? =? 0;

while(true){

num? =? s . nextint();

If(num = = 1){// ascending order

Collections.sort(list,new? Comparator & lt student & gt(){

Public? int? Comparison (student? s 1,? Student? s2)? {

int? n? =? New? integer(s 1 . get Chinese()+s 1 . get math())。 CompareTo (new? integer(S2 . get Chinese()+S2 . get math());

If (n==0)

Return? s 1.getName()。 compare to(S2 . getname());

Return? n;

}

});

PrintAll (list);

}

Or what? If(num = = 2){// descending order

Collections.sort(list,new? Comparator & lt student & gt(){

Public? int? Comparison (student? s 1,? Student? s2)? {

int? n? =? New? integer(S2 . get Chinese()+S2 . get math())。 CompareTo (new? integer(s 1 . get Chinese()+s 1 . get math());

If (n==0)

Return? s2.getName()。 compare to(s 1 . getname());

Return? n;

}

});

PrintAll (list);

}

Or what? If (quantity ==3)

Break;

other

System.out.println ("input error, re-input:");

}

Home page (list);

}

Public? Static electricity Invalid? Delete (list & lt students & gt? List){// Delete information

System.out.print ("Enter the name to delete:");

String? Name? =? s . next();

Iterator & lt student & gt? It? =? list . list iterator();

while(it.hasNext()){

Student? str? =? it . next();

if(name . equals(str . getname()){

it . remove();

System.out.println(name+"? Deleted successfully! ”);

}

}

System.out.println(" 1。 Continue? 2.Return \ Input number selection: ");

while(true){

int? num? =? s . nextint();

if(num== 1){

Delete (list);

}

Or what? If (quantity ==2)

Break;

other

System.out.println ("input error, re-input:");

}

Home page (list);

}

Public? Static electricity Invalid? print all(List & lt; Student & gt? List) {

int? num? =? 1;

System. Out.println ("-(query));

For (students? Student: List) {

System.out.println(num++? +".Name:" +stu.getName()+"| Chinese score: "+stu.getChinese()+"| Math score:" +stu.getmath ()+"-| Total score: "+(+(stu.getChinese ()+stu.getmath

}

system . out . println("-");

Home page (list);

}

Public? Static electricity Invalid? input info(List & lt; Student & gt? List){// Enter information.

System.out.println ("Enter student name:");

String? Name? =? s . next();

System.out.println ("Input Math Score:");

int? Math? =? s . nextint();

System.out.println ("Enter language score:");

int? China? =? s . nextint();

List.add (new? Students (name, math, Chinese));

System.out.println(" 1。 Return? 2. Continue to input \ nInput number selection: ");

while(true){

int? num? =? s . nextint();

if(num== 1)

Break;

Or what? If (quantity ==2)

InputInfo (list);

other

System.out.println ("input error, re-input:");

}

Home page (list);

}

}

Class? Students {

Private? String? Name;

Private? int? Chinese;

Private? int? Mathematics;

Student (string? Name, int? Math, int? Chinese) {

This name? =? Name;

This. Chinese food? =? Chinese;

This math? =? Mathematics;

}

Public? StringgetName(){

Return? Name;

}

Public? int? getChinese(){

Return? Chinese;

}

Public? int? getMath(){

Return? Mathematics;

}

}