亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

附作業代碼:用戶輸入學生數目,并為每個學生生成不重復的隨機3位數ID,讓用戶輸入學生名,實現排序,分別comparable按ID排序及comparator按姓名排

package?com.course;

import?java.util.ArrayList;
import?java.util.Collections;
import?java.util.List;
import?java.util.Random;
import?java.util.Scanner;
/**
?*?given?a?number,?auto-generated?all?the?student?ID,?which?contains?3?digits?and?is?unique
?*?for?each?ID?generating,?customer?should?enter?a?student?name
?*?1.?implement?sorting?all?the?student?record?by?their?ID?with?comparable?interface
?*?2.?implement?sorting?all?the?student?record?by?their?name?with?comparator?interface
?*?@author?richard
?*
?*/
public?class?ListSortStudent?{
????
????List<Student>?stuList?=?new?ArrayList<Student>();
????final?String?digitChar?=?"0123456789";
????
????//generate?single?student?ID?with?3?digits
????public?String?genOneID(){
????????StringBuffer?sb?=?new?StringBuffer();
????????Random?ran?=?new?Random();
????????for(int?j=0;j<3;j++){
????????????sb.append(digitChar.charAt(ran.nextInt(digitChar.length())));
????????}
????????return?sb.toString();????
????}
????
????/**
?????*?generate?a?student?list?which?contain?auto-generated?ID?and?keyboard?input?name
?????*?the?ID?should?be?unique
?????*/
????public?void?genStuList(){????????
????????Scanner?input?=?new?Scanner(System.in);
????????Scanner?input1=?new?Scanner(System.in);
????????System.out.print("Please?enter?the?number?of?student:?");
????????int?stuNum?=?input.nextInt();????????
????????
????????String?stuID,stuName;
????????List<String>?idList?=?new?ArrayList<String>();
????????Student?newStu;
????????
????????for?(int?i=0;i<stuNum;i++){
????????????stuID?=?genOneID();????????
????????????while(idList.contains(stuID)){
????????????????stuID?=?genOneID();
????????????}
????????????idList.add(stuID);
????????????System.out.println("the?auto-generated?ID?is:?"+idList.get(i));
????????????System.out.print("Please?enter?the?student?name:??");
????????????stuName?=?input1.nextLine();
????????????newStu?=?new?Student(stuName,stuID);
????????????stuList.add(newStu);
????????}
????}
????
????public?void?displayStuList(){
????????for(Student?s:stuList){
????????????System.out.println(s.getID()+"??"+s.getName());
????????}
????}
????
????public?void?sortStudentComparable(){
????????Collections.sort(stuList);
????}
????
????public?void?srotStudentComparator(){
????????Collections.sort(stuList,new?StuComparator());
????}
????public?static?void?main(String[]?args)?{
????????//?TODO?Auto-generated?method?stub
????????ListSortStudent?lst?=?new?ListSortStudent();
????????
????????lst.genStuList();
????????System.out.println("************before?sorting********");
????????lst.displayStuList();
????????System.out.println("************after?sorting?by?id**********");
????????lst.sortStudentComparable();
????????lst.displayStuList();
????????System.out.println("************after?sorting?by?name**********");
????????lst.srotStudentComparator();
????????lst.displayStuList();
????}
}

正在回答

1 回答

好牛哦

0 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消

附作業代碼:用戶輸入學生數目,并為每個學生生成不重復的隨機3位數ID,讓用戶輸入學生名,實現排序,分別comparable按ID排序及comparator按姓名排

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號