一共需要創建三個類: 1:Student類 存放程式所需的屬性和方法 2:Control類 具體的實現方法 3:Test類 測試類 Student類 Control類 Test類 ...
一共需要創建三個類:
1:Student類------存放程式所需的屬性和方法
2:Control類------具體的實現方法
3:Test類------測試類
Student類
import java.util.ArrayList;
public class Student {
String name;
int id;
ArrayList<Subject> list;
public Student() {
}
public Student(String name,int id,ArrayList<Subject> list) {
this.name = name;
this.id = id;
this.list = list;
}
@Override
public String toString() {
return "Student [name=" + name + ", id=" + id + ", list=" + list + "]";
}
}
class Subject {
String name;
int score;
public Subject() {
}
public Subject(String name,int score) {
this.name = name;
this.score = score;
}
@Override
public String toString() {
return "Subject [name=" + name + ", score=" + score + "]";
}
}
Control類
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import javax.swing.text.html.HTMLDocument.Iterator;
public class Control {
// 創建全局的list
LinkedList<Student> list = new LinkedList<Student>();
void initStudent() {
Subject subject1 = new Subject("語文",90);
Subject subject2 = new Subject("數學",80);
Subject subject3 = new Subject("英語",88);
ArrayList<Subject> list1 = new ArrayList<Subject>();
list1.add(subject1);
list1.add(subject2);
list1.add(subject3);
Student student1 = new Student("張三",1,list1);
Subject subject11 = new Subject("語文",66);
Subject subject21 = new Subject("數學",65);
Subject subject31 = new Subject("英語",99);
ArrayList<Subject> list2 = new ArrayList<Subject>();
list2.add(subject11);
list2.add(subject21);
list2.add(subject31);
Student student2 = new Student("李四",2,list2);
Subject subject12 = new Subject("語文",78);
Subject subject22 = new Subject("數學",88);
Subject subject32 = new Subject("英語",89);
ArrayList<Subject> list3 = new ArrayList<Subject>();
list3.add(subject12);
list3.add(subject22);
list3.add(subject32);
Student student3 = new Student("王五",3,list3);
Subject subject13 = new Subject("語文",66);
Subject subject23 = new Subject("數學",88);
Subject subject33 = new Subject("英語",56);
ArrayList<Subject> list4 = new ArrayList<Subject>();
list4.add(subject13);
list4.add(subject23);
list4.add(subject33);
Student student4 = new Student("趙六",4,list4);
Subject subject14 = new Subject("語文",66);
Subject subject24 = new Subject("數學",66);
Subject subject34 = new Subject("英語",83);
ArrayList<Subject> list5 = new ArrayList<Subject>();
list5.add(subject14);
list5.add(subject24);
list5.add(subject34);
Student student5 = new Student("溫九",5,list5);
this.list.add(student1);
this.list.add(student2);
this.list.add(student3);
this.list.add(student4);
this.list.add(student5);
}
//1,獲取指定學生的成績
public void score(String name) {
//遍歷list中的學生
for (Student student : this.list) {
if (name.equals(student.name)) {
List<Subject> subList = student.list;
System.out.println(subList);
}
}
}
//2,獲取指定學號的成績
public void idScore(int id) {
for (Student student : this.list) {
if (student.id == id) {
System.out.println(student.list);
}
}
}
//3,根據學生的id修改姓名
public void idChangeName(int id,String name) {
for (Student student : this.list) {
if (student.id == id) {
System.out.println("修改前的學生的姓名是:" + student.name);
student.name = name;
System.out.println("修改後的學生的姓名是:" + student.name);
}
}
}
//4,根據姓名修改指定學科的成績
public void changeScore(String name,String sub,int score) {
for (Student student : this.list) {
if (student.name.equals(name)) {
System.out.println("修改前學生的成績是:" + student.list);
for (Subject subject : student.list) {
if (subject.name.equals(sub)) {
subject.score = score;
System.out.println("修改後的學生的成績是:" + student.list);
}
}
}
}
}
//5,刪除指定學生的成績
public void delete(String name) {
for (Student student : this.list) {
if (student.name.equals(name)) {
this.list.remove();
System.out.println("刪除成功");
for (Student student1 : this.list) {
System.out.println(student1);
}
return;
}
}
}
//6,獲取到所有學生的成績,映射到新的數組中,併進行排序
public void getScore() {
ArrayList<Integer> list1 = new ArrayList<Integer>();
for (int i = 0; i < list.size(); i++) {
Student stu = list.get(i);
list1.add(stu.list.get(0).score);
}
System.out.println(list1);
for (int i = 0; i < list1.size() - 1; i++) {
for (int j = 0; j < list1.size()- i - 1; j++) {
if (list1.get(j) > list1.get(j+1)) {
int temp = list1.get(j);
list1.set(j, list1.get(j+1));
list1.set(j+1, temp);
}
}
}
System.out.println(list1);
}
//7*統計一下相同成績的人的人數
public void number() {
//創建一個HashMap來存放數組的信息,HashMap的鍵為元素,值為該元素出現的次數
HashMap<Integer, Integer> map = new HashMap<Integer,Integer>();
//從裝學生的list中取出學生信息
for (Student student : list) {
//從裝學科的list中取出各科成績
for (Subject subject : student.list) {
//System.out.println(subject);
if (subject.name == "語文") {
Integer num = map.get(subject.score);
map.put(subject.score, num == null ? 1 : num + 1);
}
}
}
Set keySet = map.keySet(); //用集合接收穫取所有的鍵
for (Object object : keySet) {
System.out.print("成績:" + object);
System.out.print(",");
System.out.println("出現的次數: " + map.get(object));
}
}
}
Test類
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
//先創建一個對象
Control control = new Control();
control.initStudent();
//1,
System.out.println("請輸入學生的姓名:");
Scanner sc = new Scanner(System.in);
String name = sc.next();
control.score(name);
//2,
System.out.println("請輸入學生的id:");
int id = sc.nextInt();
control.idScore(id);
//3,
System.out.println("請輸入學生的id:");
int id1 = sc.nextInt();
System.out.println("請輸入要修改的姓名:");
String name1 = sc.next();
control.idChangeName(id1, name1);
//4,
control.changeScore("張三", "語文", 100);
//5
control.delete("張三");
//6,
control.getScore();
//7,
control.number();
}
}