java實現學生資訊統計系統
阿新 • • 發佈:2019-01-30
學生資訊查詢
功能:
1.通過id修改學生資訊
2.獲取指定學生的成績
3.獲取指定學號的成績
4.根據姓名修改指定學科的成績
5.刪除指定學生及其成績
6.統計所有成績相等的人數
7.獲取到所有學生某一科的成績
程式碼塊
package com.lanou.work01;
import java.util.ArrayList;
import java.util.HashSet;
public class ProgramSystem {
public static void main(String[] args) {
ArrayList<Student> listStudent = new ArrayList<Student>();
// 學生小李
Student student = new Student(1, "小李");
Subject subject1 = new Subject("語文", 90.7);
Subject subject2 = new Subject("數學", 10.5);
student.list.add(subject1);
student.list.add(subject2);
// 學生小王
Student student2 = new Student(1, "小王");
Subject subject3 = new Subject("語文", 20.7);
Subject subject4 = new Subject("數學", 10.5);
student2.list.add(subject3);
student2.list.add(subject4);
// 裝進大的陣列
listStudent.add(student);
listStudent.add(student2);
System.out.println(listStudent.size());
Control control = new Control(listStudent);
//以下注釋內容為功能測試
// control.getScore(student, "語文");
// System.out.println("-------------------------");
// control.getId(1, "數學");
// System.out.println("-------------------------");
// control.upDateName(1, "小陳");
// System.out.println(student.nameString);
// System.out.println("-------------------------");
// control.upDateScore("小陳", "語文", 0.0);
// System.out.println(student);
// control.removeStudent(student);
// System.out.println(listStudent);
// control.countStudnet(student, "語文");
control.fun("語文");
}
}
class Control {
// 存放學生的陣列
ArrayList<Student> listStudent = new ArrayList<Student>();
public Control(ArrayList<Student> listStudent) {
this.listStudent = listStudent;
}
//展示方法
public void show(){
System.out.println("*************************");
}
// 獲取指定學生的成績
public void getScore(Student student, String bookName) {
for (Student student2 : listStudent) {
if (student.nameString.equals(student2.nameString)) {
for (Subject sub : student.list) {
// 每一個成績類物件
if (bookName.equals(sub.nameBook)) {
System.out.println(sub.Score);
}
}
}
}
}
// 獲取指定學號的成績
public void getId(int id, String bookName) {
for (Student student2 : listStudent) {
if (student2.id == id) {// 如果id相等話
for (Subject sub : student2.list) {
// 每一個成績類物件
if (bookName.equals(sub.nameBook)) {
System.out.println(sub.Score);
}
}
}
}
}
// 根據id修改姓名
public void upDateName(int id, String name) {
for (Student student : listStudent) {
if (student.id == id) {// 如果想等的話
student.nameString = name;
}
}
}
// 根據姓名修改指定學科的成績
public void upDateScore(String name, String bookName, Double score) {
for (Student student : listStudent) {
if (student.nameString.equals(name)) {
for (Subject subject : student.list) {
if (subject.nameBook.equals(bookName)) {
subject.Score = score;
}
}
}
}
}
// 刪除指定學生及其成績
public void removeStudent(Student student) {
Student student3 = new Student();
System.out.println(listStudent);
for (Student student2 : listStudent) {
if (student2.nameString.equals(student.nameString)) {
student3 = student2;
}
}
listStudent.remove(student3);
}
// 統計所有成績相等的人數
public void countStudnet(Student student, String namString) {
int count = 1;
// 建立一個數組
double array[] = new double[listStudent.size()];
System.out.println(listStudent.size());
for (Student student2 : listStudent) {
for (int i = 0; i < array.length; i++) {
System.out.println("陣列的長度"+array.length);
for (Subject subject : student2.list) {
if (subject.nameBook.equals(namString)) {
System.out.println(namString);
array[i] = subject.Score;
System.out.println(array[i]);
}
}
}
}
// 第一個和第二個比較
for (int i = 0; i < array.length; i++) {
for (int j = i + 1; j < array.length; j++) {
if (array[i] == array[j]) {
count++;
}
}
}
System.out.println("語文成績相等的人數為:" + count);
}
// 獲取到所有學生的成績
public void fun(String nameString) {
HashSet<Double> set = new HashSet<Double>();
for (Student student2 : listStudent) {
for (Subject subject : student2.list) {
if (nameString.equals(subject.nameBook)) {
set.add(subject.Score);
}
}
}
System.out.println(set);
}
}
//學生類
class Student {
String nameString;
int id;
ArrayList<Subject> list = new ArrayList<Subject>();
public Student(int id, String nameString) {
this.id = id;
this.nameString = nameString;
}
@Override
public String toString() {
// TODO Auto-generated method stub
for (Subject subject : list) {
return "姓名:" + nameString + "學號:" + id + "成績單:" + subject.nameBook
+ subject.Score;
}
return null;
}
public Student() {
}
}
// 學科類
class Subject {
String nameBook;
double Score;
public Subject(String nameBook, double score) {
super();
this.nameBook = nameBook;
Score = score;
}
}