--- tags: Cmoney_Java題目 --- Java_Cmoney_st208 ===    1.需要的 function 和 Class --- 1.1 學生 Class 包含他的座號和缺席紀錄 --- 因為是在答題系統哩,Class 建立到同一個.java 裡,不用 public 1. 2個屬性(seat absence),1個方法(print:印"座號.缺席次數") 2. 為方便不用 getter ```java= class Stu { int seat; int absence; Stu(int seat, int absence) { this.seat = seat; this.absence = absence; } void print() { System.out.println(this.seat + "." + this.absence); } } ``` 1.2 使用座號,從學生陣列中取得該學生物件 --- ```java= public static Stu getStu(Stu[] stus, int seat) { for (int i = 0; i < 10; i++) { if (stus[i].seat == seat) return stus[i]; } return null; } ``` 2.主程式 --- 1. 創建有10個學生物件的陣列 2. 利用迴圈,實體那10個學生 3. 迴圈,連續輸入5天的缺席狀況,int n 代表有幾筆缺席紀錄 如果是-1代表沒有缺席紀錄,用`continue;`跳到下一天 4. 迴圈,連續讀取 n 個缺席學生座號,利用讀取到座號, 使用 getStu(stus,seat),然後直接操作實體屬性(缺席)++ 5. 最後用迴圈印出所有學生的資料 ```java= public static void main(String[] args) { Scanner sc = new Scanner(System.in); Stu[] stus = new Stu[10]; for (int i = 0; i < 10; i++) { stus[i] = new Stu(i + 1, 0); } for (int i = 0; i < 5; i++) { int n = sc.nextInt(); if (n == -1) continue; for (int j = 0; j < n; j++) { getStu(stus, sc.nextInt()).absence++; } } for (int i = 0; i < 10; i++) { stus[i].print(); } } ``` 3.完整程式 --- ```java= import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); Stu[] stus = new Stu[10]; for (int i = 0; i < 10; i++) { stus[i] = new Stu(i + 1, 0); } for (int i = 0; i < 5; i++) { int n = sc.nextInt(); if (n == -1) continue; for (int j = 0; j < n; j++) { getStu(stus, sc.nextInt()).absence++; } } for (int i = 0; i < 10; i++) { stus[i].print(); } } public static Stu getStu(Stu[] stus, int seat) { for (int i = 0; i < 10; i++) { if (stus[i].seat == seat) return stus[i]; } return null; } } class Stu { int seat; int absence; Stu(int seat, int absence) { this.seat = seat; this.absence = absence; } void print() { System.out.println(this.seat + "." + this.absence); } } ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up