---
# System prepended metadata

title: Java_Cmoney_ft8107
tags: [Cmoney_Java題目]

---

---
tags: Cmoney_Java題目
---
Java_Cmoney_ft8107
===

![](https://i.imgur.com/wWuZOum.png)
![](https://i.imgur.com/8hXg6Tt.png)



1.需要的 function 和 class
---
1.1 class Stu
---
1. attribute : seat, score
2. method : print()
```java=
class Stu {
    int seat;
    int score;

    Stu(int seat, int score) {
        this.seat = seat;
        this.score = score;
    }

    void print() {
        System.out.println(seat + "." + score);
    }
}
```


1.2 增加陣列空間
---
之前都是用乘2的方式，雖然一直+1有點耗能，
但可以確保 stus.length == count，
如果用乘2的方式，其他用到 stus.length 的地方都要換成 count，
要不然會一直跑 nullpointerexception 。

```java=
public static Stu[] doubleArr(Stu[] stus) {
        Stu[] newStus = new Stu[stus.length + 1];
        for (int i = 0; i < stus.length; i++) {
            newStus[i] = stus[i];
        }
        return newStus;
    }
```


1.3 交換和泡沫排序
---
常犯錯誤
1. sortScore() 裡的 i 和 j 寫反
2. swap() 忘記用 tmp 寫成 `stus[i2] = stus[i1] ;`
**==這題如果分數一樣，也要交換，想不到吧==**
![](https://i.imgur.com/HkB1gBb.png)

```java=
public static void sortScore(Stu[] stus) {
        for (int i = 0; i < stus.length - 1; i++) {
            for (int j = 0; j < stus.length - i - 1; j++) {
                if (stus[j].score >= stus[j + 1].score)
                    swap(stus, j, j + 1);
            }
        }
    }

public static void swap(Stu[] stus, int i1, int i2) {
        Stu tmp = stus[i1];
        stus[i1] = stus[i2];
        stus[i2] = tmp;
    }
```


1.4 全部印出(順序不同)
---
```java=
public static void printAlltoBig(Stu[] stus) {
        for (int i = 0; i < stus.length; i++) {
            stus[i].print();
        }
    }

public static void printAlltoSmall(Stu[] stus) {
        for (int i = stus.length - 1; i > -1; i--) {
            stus[i].print();
        }
    }
```


2.主程式
---


```java=
public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Stu[] stus = new Stu[1];
        int count = 0;
        while (true) {
            int score = sc.nextInt();
            if (score == -1)
                break;
            if (count == stus.length)
                stus = doubleArr(stus);
            stus[count] = new Stu(count + 1, score);
            count++;
        }
        sortScore(stus);
        int dir = sc.nextInt();
        if (dir == 1)
            printAlltoBig(stus);
        else
            printAlltoSmall(stus);
    }
```

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[1];
        int count = 0;
        while (true) {
            int score = sc.nextInt();
            if (score == -1)
                break;
            if (count == stus.length)
                stus = doubleArr(stus);
            stus[count] = new Stu(count + 1, score);
            count++;
        }
        sortScore(stus);
        int dir = sc.nextInt();
        if (dir == 1)
            printAlltoBig(stus);
        else
            printAlltoSmall(stus);
    }

    public static void sortScore(Stu[] stus) {
        for (int i = 0; i < stus.length - 1; i++) {
            for (int j = 0; j < stus.length - i - 1; j++) {
                if (stus[j].score >= stus[j + 1].score)
                    swap(stus, j, j + 1);
            }
        }
    }

    public static void swap(Stu[] stus, int i1, int i2) {
        Stu tmp = stus[i1];
        stus[i1] = stus[i2];
        stus[i2] = tmp;
    }

    public static void printAlltoBig(Stu[] stus) {
        for (int i = 0; i < stus.length; i++) {
            stus[i].print();
        }
    }

    public static void printAlltoSmall(Stu[] stus) {
        for (int i = stus.length - 1; i > -1; i--) {
            stus[i].print();
        }
    }

    public static Stu[] doubleArr(Stu[] stus) {
        Stu[] newStus = new Stu[stus.length + 1];
        for (int i = 0; i < stus.length; i++) {
            newStus[i] = stus[i];
        }
        return newStus;
    }
}

class Stu {
    int seat;
    int score;

    Stu(int seat, int score) {
        this.seat = seat;
        this.score = score;
    }

    void print() {
        System.out.println(seat + "." + score);
    }
}
```