今天練習了一個題目-學生資訊系統
功能:
輸入1-6來決定功能
1.新增學生資料 2.輸入座位印出成績 3.成績高至低列印 4.座位低至高印出 5.輸入座位更改成績 6.結束程式
要思考更多,像是功能要放在哪一個類別之類的
以下main程式
import java.util.Scanner; public class MainClass { public static void main(String[] args) { Scanner sc = new Scanner(System.in); Student std[] = new Student[2]; int count = 0; boolean toggle = true; //功能選單開關 while (toggle) { System.out.print("請選擇服務項目:\n1.新增學生資料\t2.印出個人成績\t3.依照成績高至低排列印出\n" + "4.依照座位低至高排列印出\t5.更改成績\t6.結束程式\n"); int select = sc.nextInt(); switch (select) { case 1: if (count == std.length) { //陣列加長控制 std = doubleArray(std); } std[count++] = newStudent(); //將物件放入Student陣列中 break; case 2: System.out.println("請輸入要列印之座號:"); int seat = sc.nextInt(); for (int i = 0; i < count; i++) { //尋找陣列中有無跟輸入值一樣的座位號碼 if (std[i].getSeat() == seat) std[i].printScore(); //有的話印出來 break; } System.out.println("查無此座號"); break; case 3: bigToSmall(std, count); //排列大到小 for (int i = 0; i < count; i++) { std[i].printAll(); } break; case 4: smallToBigSeat(std, count); //排列小到大 for (int i = 0; i < count; i++) { std[i].printAll(); } break; case 5: System.out.print("請輸入更改成績之座號:"); seat = sc.nextInt(); for (int i = 0; i < count; i++) { //尋找陣列中有無跟輸入值一樣的座位號碼 if (std[i].getSeat() == seat) { System.out.print("請輸入新成績:"); int newScore = sc.nextInt(); std[i].changeScore(newScore); break; } } System.out.println("查無此座號"); break; case 6: toggle = false; break; } } System.out.print("程式已關閉"); } public static Student[] doubleArray(Student std[]) { Student newStd[] = new Student[(std.length * 2)]; System.arraycopy(std, 0, newStd, 0, std.length); return newStd; } public static Student newStudent() { //建立Student物件 Scanner sc = new Scanner(System.in); System.out.print("請輸入班級:"); String classNum = sc.nextLine(); System.out.print("請輸入姓名:"); String name = sc.nextLine(); System.out.print("請輸入座號:"); int seat = sc.nextInt(); System.out.print("請輸入分數:"); int score = sc.nextInt(); Student std = new Student(classNum, name, seat, score); return std; } public static void swap(Student[] std, int a, int b) { Student tmp[] = new Student[1]; tmp[0] = std[a]; std[a] = std[b]; std[b] = tmp[0]; } public static void bigToSmall(Student std[], int count) { for (int i = 0; i < count; i++) { for (int j = 1; j < count - i; j++) { if (std[i].getScore() < std[i + j].getScore()) { //判斷物件的成績大小 swap(std, i, (i + j)); } } } } public static void smallToBigSeat(Student std[], int count) { for (int i = 0; i < count; i++) { for (int j = 1; j < count - i; j++) { if (std[i].getSeat() > std[i + j].getSeat()) { swap(std, i, (i + j)); } } } } public static void smallToBig(Student std[], int count) { for (int i = 0; i < count; i++) { for (int j = 1; j < count - i; j++) { if (std[i].getScore() > std[i + j].getScore()) { swap(std, i, (i + j)); } } } } }
Student 類別
import java.util.Scanner; public class Student { private int score; private String classNum; private String name; private int seat; //建構子 public Student(String classNum, String name, int seat, int score) { setClassNum(classNum); setName(name); setSeat(seat); setScore(score); } public Student(String classNum, String name, int seat) { this(classNum,name,seat,0); } public void printScore() { System.out.println(this.score + "分"); } public void printAll(){ System.out.print("班級:" + this.getClassNum() + "\t座號:" + this.getSeat() + "\t姓名:" + this.getName() + "\t" + this.getScore() + "分\n" ); } public void changeScore(int newScore){ setScore(newScore); } public int getScore() { return score; } private void setScore(int score) { if (score > 100) { this.score = 100; }else if (score < 0) { this.score = 0; } else { this.score = score; } } public String getClassNum() { return classNum; } private void setClassNum(String classNum) { this.classNum = classNum; } public String getName() { return name; } private void setName(String name) { this.name = name; } public int getSeat() { return seat; } private void setSeat(int seat) { this.seat = seat; } }
全站熱搜
留言列表