- 最後登錄
- 2024-11-16
- 在線時間
- 7253 小時
- 註冊時間
- 2010-5-4
- 閱讀權限
- 95
- 精華
- 0
- UID
- 7964333
- 帖子
- 155
- 積分
- 1282 點
- 潛水值
- 47975 米
| 如果瀏覽伊莉時速度太慢或無法連接,可以使用其他分流瀏覽伊莉,www01.eyny.com(02,03)。 本帖最後由 tryit244178 於 2019-12-4 10:43 AM 編輯
參考看看吧- import java.util.Scanner;
- public class Score {
- public static void main(String[] args) {
- int[] sr = {15, 20, 64, 90, 81, 52, 78, 12, 64, 75, 43, 78, 35, 47, 97};
- Manager m = new Manager();
- m.Start(sr);
- }
- }
- class Manager{
- public void Start(int[] sr) {
- int select = 3;
- ScoreSystem[] ss = Initilize(sr);
-
- do {
- select = ShowMenu();
- if (IsSelect(select))
- ss[select].Start();
- } while (select!=3);
- }
-
- private ScoreSystem[] Initilize(int[] sr) {
- ScoreSystem[] ss = {new ShowScore(sr),
- new SeachScore(sr),
- new ModifyScore(sr)};
- return ss;
- }
-
- private int ShowMenu() {
- System.out.println("\n------選單------\n"+
- "1: 顯示成績\n"+
- "2: 查詢成績\n"+
- "3: 修改成績\n"+
- "4: 離開作業");
- System.out.print("選擇作業(1 到 4)==> ");
- Scanner sc = new Scanner(System.in);
- int result = sc.nextInt();
- return --result;
- }
-
- private boolean IsSelect(int select) {
- if (select>-1 && select<3)
- return true;
- return false;
- }
- }
- abstract class ScoreSystem{
- int[] sr;
-
- abstract void Start();
- int EnterSN(String s) {
- System.out.print("選擇要" + s + "的學號(1 到 " + sr.length + ")==> ");
- Scanner sc = new Scanner(System.in);
- return sc.nextInt();
- }
-
- boolean IsSN(int sn) {
- if (sn>0 && sn<=sr.length)
- return true;
- return false;
- }
- }
- class ShowScore extends ScoreSystem{
- public ShowScore(int[] source) {
- sr = source;
- }
-
- @Override
- public void Start() {
- System.out.println("\n顯示成績:");
- for (int i=0; i<sr.length; i++) {
- System.out.println("學號 " + (i+1) + "\t,成績:" + sr[i]);
- }
- }
- }
- class SeachScore extends ScoreSystem {
- public SeachScore(int[] source) {
- sr = source;
- }
-
- @Override
- public void Start() {
- System.out.println("\n查詢成績");
- int sn = EnterSN("查詢");
- if (IsSN(sn))
- System.out.println("\n學號 " + sn + "\t,成績:" + sr[--sn]);
- }
- }
- class ModifyScore extends ScoreSystem {
- public ModifyScore(int[] source) {
- sr = source;
- }
-
- @Override
- public void Start() {
- System.out.println("\n修改成績");
- int sn = EnterSN("修改");
- if (IsSN(sn)) {
- System.out.print("\n原成績:" + sr[--sn] + "\t");
- Modify(sn);
- }
- }
-
- private void Modify(int sn) {
- System.out.print("輸入新成績==>");
- Scanner sc = new Scanner(System.in);
- sr[sn] = sc.nextInt();
- }
- }
複製代碼
... |
|