---
title: JAVA(程設-期中考前)
tags: JAVA(程設-期中考前)
---
# JAVA程式語言 --- 李鍾斌(程設-期中考前)
> 【目次】
> [TOC]
>
> Reference website:
> 1. [程式語言的歷史資訊圖表:Java 才是現任霸主](http://technews.tw/2013/12/17/programming-languages/)
> 2. [程式語言的歷史演進 + 圖表 1843 ~ 2013](https://blog.longwin.com.tw/2013/12/programming-language-history-1843-2013/)
> 3. [DrJava--免安裝](http://www.drjava.org/)
>
---
# C++ vs. JAVA
* C++ 可直接讀取電腦記憶體中的資料(操作風險)
* JAVA 不可對記憶體直接操作
---
# DrJava 操作步驟:
## 1. 建立類別 class



## 2. Compile:確認是否有語法錯誤


## 3. Run:執行

---
# Eclipse
1. 建立 project


2. 建立 class


3. Run

* 更改字體大小

---
* println (有換行) vs. print (沒換行)

* 小程式
|資料類型 | |資料類型 | |
| -------- | ----| -------- |-------- |
| int | 整數 | long | 長整數 |
| float | 小數 | double | 倍精度小數 |
| char | 字元 | string | 字串 |
| boolean | 布林 |




## HW: BMI計算器
```java=
import java.util.*;
public class Week02 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("PLEASE INPUT height (cm): ");
double height = sc.nextDouble();
System.out.println("PLEASE INPUT weight (kg): ");
double weight = sc.nextDouble();
height = height/100;
double bmi = weight/(height*height);
System.out.println("BMI = " + bmi); //BMI = 體重(公斤) / 身高2(公尺2)
}
}
```
---
# 2018-10-01 class
## if - else 條件式
```java=
if(條件式1){
... //符合條件時,要執行的內容
}
else if(條件式2){
... //符合條件時,要執行的內容
}
else{
... //不符合條件時,要執行的內容
}
> < >= <=
a==b 相等 vs. a=b 給值
a!=b 不相等
&& and 且
|| or 或
```
* BMI 加上條件式--簡潔版
```java=
import java.util.*;
public class Week03 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("PLEASE INPUT height (cm): ");
double height = sc.nextDouble();
System.out.println("PLEASE INPUT weight (kg): ");
double weight = sc.nextDouble();
height = height/100;
double bmi = weight/(height*height);
System.out.println("BMI = " + bmi); //BMI = 體重(公斤) / 身高2(公尺2)
if (bmi < 20)
System.out.println("體重過輕");
else if (bmi < 25)
System.out.println("體重正常");
else if (bmi < 30)
System.out.println("體重過重");
else
System.out.println("go to see a doctor");
}
}
```
* BMI 加上條件式--麻煩版
```java=
if (bmi < 20)
System.out.println("體重過輕");
else
if (bmi < 25 && bmi >= 20)
System.out.println("體重正常");
else
if (bmi < 30 && bmi >= 25)
System.out.println("體重過重");
else
System.out.println("go to see a doctor");
```
* Python可以這樣用!!!
```python=
bmi = 24
if bmi < 20:
print("體重過輕")
elif (20 <= bmi < 25):
print("體重正常")
elif (30 > bmi >= 25):
print("體重過重")
else:
print("go to see a doctor")
```
## switch 條件式
```java=
import java.util.*;
public class Week02 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("PLEASE INPUT number: ");
int sex = sc.nextInt();
switch(sex){
case 1:
System.out.println("boy");
break;
case 2:
System.out.println("girl");
break;
default:
System.out.println("error");
}
```
## HW: 利用機車排氣量,計算牌照稅、燃料稅、total!!!
```java=
import java.util.*;
public class tax {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("PLEASE INPUT : 機車排氣量");
double engine_displacement = sc.nextDouble(); //機車排氣量
int license_tax= 0; //牌照稅
int fuel_tax = 0; //燃料稅
int total_tax = license_tax + fuel_tax;
if (engine_displacement <= 50){
license_tax=0;
fuel_tax=300;
}
else if (engine_displacement <= 125){
license_tax=0;
fuel_tax=450;
}
else if (engine_displacement <= 150){
license_tax=0;
fuel_tax=600;
}
else if (engine_displacement <= 250){
license_tax=800;
fuel_tax=600;
}
else if (engine_displacement <= 500){
license_tax=1620;
fuel_tax=900;
}
else if (engine_displacement <= 600){
license_tax=2160;
fuel_tax=1200;
}
else if (engine_displacement <= 1200){
license_tax=4320;
fuel_tax=1800;
}
else if (engine_displacement <= 1800){
license_tax=7120;
fuel_tax=2010;
}
else{
license_tax=11230;
fuel_tax=2010;
}
System.out.println("Vehicle License Tax + Fuel Tax = " + total_tax);
System.out.println(license_tax + " + " + fuel_tax + " = " + total_tax);
}
}
```
# 2018-10-08 class
## for 迴圈
```java=
for(旗標變數的起始值; 迴圈繼續執行的條件; 旗標變數的改變量){
... //迴圈要執行的內容
}
```
```java=
public class Week5 {
public static void main(String[] args) {
int i;
for(i=1; i<=5; i=i+1){
System.out.println("i=" + i);
}
}
}
Output:
i=1
i=2
i=3
i=4
i=5
```
```java=
public class Week5 {
public static void main(String[] args) {
int i; //迴圈使用的旗標變數
int sum = 0; //用來計算總合的變數
for(i=1;i<=10;i=i+1){
System.out.println("i=" + i);
sum = sum + i; //將旗標變數 目前的內容值加到sum中
}
System.out.println("Sum = " + sum);
}
}
Output:
i=1
i=2
i=3
i=4
i=5
i=6
i=7
i=8
i=9
i=10
Sum = 55
```
```java=
import java.util.*;
public class Week5 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("PLEASE INPUT ANY INTERGER: ");
int num = sc.nextInt(); //輸入整數,之後會做加總
int i; //迴圈使用的旗標變數
int sum = 0; //用來計算總合的變數
for(i=1;i<=num;i=i+1){
System.out.println("i=" + i);
sum = sum + i; //將旗標變數 目前的內容值加到sum中
}
System.out.println("Sum = " + sum);
}
}
Output:
PLEASE INPUT ANY INTERGER: [DrJava Input Box = 3]
i=1
i=2
i=3
Sum = 6
```
## 印出指定項次的費氏數列
```java=
import java.util.*;
public class Week5 {
public static void main(String[] args) {
int i; //迴圈使用的旗標變數
int pre1=0,pre2=0,current; //用來保存費氏數列的前項,前二項,目前項
Scanner sc = new Scanner(System.in);
System.out.println("請輸入項次: ");
int num = sc.nextInt();
for(i=1; i<=num; i++){
if(i==1)
System.out.println("F(1)=1");
else if (i==2){
System.out.println("F(2)=1");
pre1 = 1; pre2 = 1; //先為i=3時的前項與前二項做準備
}
else{
current = pre1 + pre2; //費氏數列的第三項起 數值為前兩項和
System.out.println("F(" + i + ") = " + current);
pre2 = pre1; //迴圈進入下一輪之前,將本輪的前項值交給前二項
pre1 = current; //將本輪的current值交給前項
} //end of else
} //end of for
}
}
Output:
請輸入項次: [DrJava Input Box = 5]
F(1)=1
F(2)=1
F(3) = 2
F(4) = 3
F(5) = 5
```
## HW: 印出等差數列
```java=
#方法一
import java.util.*;
public class Week5 {
public static void main(String[] args) {
int i; //迴圈使用的旗標變數
int pre1=0,pre2=0,current; //用來保存費氏數列的前項,前二項,目前項
Scanner sc = new Scanner(System.in);
System.out.println("請輸入首項: "); //2
System.out.println("請輸入項數: "); //5
System.out.println("請輸入公差: "); //3
int a = sc.nextInt(); //2,5,8,11,14
int n = sc.nextInt(); //a,a+d,a+d+d,a+d+d+d,a+d+d+d+d
int d = sc.nextInt();
for(i=1; i<=n; i++){
if (i==1) //第一輪 印出 首項
System.out.println(a);
else if (i==2){
a = a+d; //第二輪 執行 首項+公差
System.out.println(a);
}
else {
a = a+d; //第三輪以後 執行 首項+公差
System.out.println(a);
}
} //end of for
}
}
#課堂上雖已繳交,不過我還是想把原本想做的方式做出來,故此版本為方法二
import java.util.*;
public class week5 {
public static void main(String[] args) {
int k; //用來計算當前該項之公差的係數
int current = 0; //當前該項數的數值
Scanner sc = new Scanner(System.in);
System.out.println("輸入首項");
int a = sc.nextInt(); //首項=2
System.out.println("輸入項數");
int n = sc.nextInt(); //項數=5
System.out.println("輸入公差");
int d = sc.nextInt(); //公差=3
for (k=0; k<=n-1; k++) {
current = a + k * d;
//公式: 當前該項數的數值 = 首項 + 係數 * 公差
//2=2+0*3, 5=2+1*3, 8=2+2*3, 11=2+3*3, 14=2+4*3
System.out.println(current);
}
}
}
```
# 2018-10-15 class
## for 迴圈 (排版美觀)
```java=
##方法一
import java.util.*;
public class Week06 {
public static void main(String[] args) {
for (int i=1; i<=9; i++){
for (int j=1; j<=9; j++){
System.out.print(i + "*" + j + "=" + (i*j) + " ");
}
System.out.println();
}
}
}
##方法二
for (int i=1; i<=9; i++){
for (int j=1; j<=9; j++){
System.out.printf("%d * %d = %d ",i ,j ,i*j);
}
System.out.println();
}
Output:
1*1=1 1*2=2 1*3=3 1*4=4 1*5=5 1*6=6 1*7=7 1*8=8 1*9=9
2*1=2 2*2=4 2*3=6 2*4=8 2*5=10 2*6=12 2*7=14 2*8=16 2*9=18
3*1=3 3*2=6 3*3=9 3*4=12 3*5=15 3*6=18 3*7=21 3*8=24 3*9=27
4*1=4 4*2=8 4*3=12 4*4=16 4*5=20 4*6=24 4*7=28 4*8=32 4*9=36
5*1=5 5*2=10 5*3=15 5*4=20 5*5=25 5*6=30 5*7=35 5*8=40 5*9=45
6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36 6*7=42 6*8=48 6*9=54
7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49 7*8=56 7*9=63
8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64 8*9=72
9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81
```
* %d 整數
* %f 小數
* %s 字串
* %c 字元
* \n 換行
* \t 跳到下個定位點
```java=
##方法三
for (int i=1; i<=9; i++){
for (int j=1; j<=9; j++){
System.out.printf("%2d*%2d=%2d ",i ,j ,i*j); //用兩位數的寬度來放資料
}
System.out.printf("\n"); //= System.out.println();
}
Output:
1* 1= 1 1* 2= 2 1* 3= 3 1* 4= 4 1* 5= 5 1* 6= 6 1* 7= 7 1* 8= 8 1* 9= 9
2* 1= 2 2* 2= 4 2* 3= 6 2* 4= 8 2* 5=10 2* 6=12 2* 7=14 2* 8=16 2* 9=18
3* 1= 3 3* 2= 6 3* 3= 9 3* 4=12 3* 5=15 3* 6=18 3* 7=21 3* 8=24 3* 9=27
4* 1= 4 4* 2= 8 4* 3=12 4* 4=16 4* 5=20 4* 6=24 4* 7=28 4* 8=32 4* 9=36
5* 1= 5 5* 2=10 5* 3=15 5* 4=20 5* 5=25 5* 6=30 5* 7=35 5* 8=40 5* 9=45
6* 1= 6 6* 2=12 6* 3=18 6* 4=24 6* 5=30 6* 6=36 6* 7=42 6* 8=48 6* 9=54
7* 1= 7 7* 2=14 7* 3=21 7* 4=28 7* 5=35 7* 6=42 7* 7=49 7* 8=56 7* 9=63
8* 1= 8 8* 2=16 8* 3=24 8* 4=32 8* 5=40 8* 6=48 8* 7=56 8* 8=64 8* 9=72
9* 1= 9 9* 2=18 9* 3=27 9* 4=36 9* 5=45 9* 6=54 9* 7=63 9* 8=72 9* 9=81
```
```java=
##方法四
for (int i=1; i<=9; i++){
for (int j=1; j<=9; j++){
System.out.printf("%d*%d=%d\t",i ,j ,i*j); // \t 跳到下個定位點
}
System.out.printf("\n");
}
Output:
1*1=1 1*2=2 1*3=3 1*4=4 1*5=5 1*6=6 1*7=7 1*8=8 1*9=9
2*1=2 2*2=4 2*3=6 2*4=8 2*5=10 2*6=12 2*7=14 2*8=16 2*9=18
3*1=3 3*2=6 3*3=9 3*4=12 3*5=15 3*6=18 3*7=21 3*8=24 3*9=27
4*1=4 4*2=8 4*3=12 4*4=16 4*5=20 4*6=24 4*7=28 4*8=32 4*9=36
5*1=5 5*2=10 5*3=15 5*4=20 5*5=25 5*6=30 5*7=35 5*8=40 5*9=45
6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36 6*7=42 6*8=48 6*9=54
7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49 7*8=56 7*9=63
8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64 8*9=72
9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81
```
## while 迴圈 (事先檢查。上車收票)
```java=
for (i=1; i<=n; i++){ //使用時機:已知該跑幾次
...
}
VS. //目前兩種相同
int i=1;
while (i<=n){ //使用時機:不知該跑幾次,但知道條件
...
i++;
}
```
```java=
int i=1;
while (i<=9){
int j=1; //notce! j要放裡面
while (j<=9){
System.out.printf("%d*%d=%d\t",i,j,i*j);
j++;
}
System.out.printf("\n");
i++;
}
```
## do ... while 迴圈 (先做再說,一定會做完第一次。下車收票)
```java=
int i;
do {
...
i++;
} while (i<=n);
```
---
```java=
for (int i=1; i<=5; i++){
for (int j=1; j<=i; j++){
System.out.printf("%d*%d=%d\t",i ,j ,i*j); // \t 跳到下個定位點
}
System.out.printf("\n");
}
Output:
1*1=1
2*1=2 2*2=4
3*1=3 3*2=6 3*3=9
4*1=4 4*2=8 4*3=12 4*4=16
5*1=5 5*2=10 5*3=15 5*4=20 5*5=25
```
```java=
Scanner sc = new Scanner(System.in);
System.out.println("please enter integer: ");
int n = sc.nextInt();
for (int i=1; i<=n; i++){
for (int j=1; j<=i; j++){
System.out.printf("正"); // \t 跳到下個定位點
}
System.out.printf("\n");
}
Output:
please enter integer: [DrJava Input Box =4]
正
正正
正正正
正正正正
```
```java=
Scanner sc = new Scanner(System.in);
System.out.println("please enter integer: ");
int n = sc.nextInt();
for (int i=n; i>=1; i--){
for (int j=1; j<=i; j++){
System.out.printf("正"); // \t 跳到下個定位點
}
System.out.printf("\n");
}
Output:
please enter integer: [DrJava Input Box =4]
正正正正
正正正
正正
正
```
```java=
Scanner sc = new Scanner(System.in);
System.out.println("please enter integer: ");
int n = sc.nextInt();
for (int i=1; i<=n; i++){
for (int j=1; j<=n-i; j++){
System.out.print("_ "); // 印__的內圈
}
for (int j=1; j<=i; j++){
System.out.print("正"); // 印正的內圈
}
System.out.print("\n");
}
Output:
please enter integer: [DrJava Input Box =4]
_ _ _ 正
_ _ 正正
_ 正正正
正正正正
```
## HW: 用 for 迴圈印出固定格式
```java=
Scanner sc = new Scanner(System.in);
System.out.println("please enter integer: ");
int n = sc.nextInt();
for (int i=0; i<=n-1; i++){
for (int j=1; j<=i; j++){
System.out.print("白"); // 印"白"的內圈
}
for (int j=1; j<=n-i; j++){
System.out.print("星"); // 印"星"的內圈
}
System.out.print("\n");
}
Output:
please enter integer: [DrJava Input Box =4]
星星星星星
白星星星星
白白星星星
白白白星星
白白白白星
```
# 2018-10-22 class
## 陣列 array (matrix)
* 現有能力:用"變數"來存取n次 (共n筆資料)
運用陣列 --> 省時省力
* java 儲存同質性的資料型態 vs. python 可塞不同的資料型態
### 一維陣列
* int [] array_test = new int[n]; //依據使用者需求宣告陣列大小
```java=
//未知初始值
int [] array_test = new int [10]; // 10個可以放整數大小的記憶體空間。
//array[0]~array[9]
for (int i=0; i<10; i++)
array_test[i] = i*2;
for (int i=0; i<10; i++)
System.out.println("array["+i+"]="+array_test[i]);
```
```java=
//已知初始值
int [] array_test = {1,2,3,4,5,6,7,8,9,10};
// for (int i=0; i<10; i++)
// array_test[i] = i*2;
for (int i=0; i<10; i++)
System.out.println("array["+i+"]="+array_test[i]);
```
```java=
import java.util.*;
public class Week07 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("資料筆數:");
int n = sc.nextInt();
int [] array_test = new int[n]; //依據使用者需求宣告陣列大小
for (int i=0; i<n; i++) {
System.out.printf("請輸入第%d筆資料",i+1);
array_test[i] = sc.nextInt();
}
for (int i=0; i<n; i++) {
System.out.println("array["+i+"]="+array_test[i]); //the same
System.out.printf("array[%d]=%d\n",i,array_test[i]);
}
}
}
Output:
資料筆數: [DrJava Input Box = 2]
請輸入第1筆資料 [DrJava Input Box = 8]
請輸入第2筆資料 [DrJava Input Box = 6]
array[0]=8
array[1]=6
```
```java=
import java.util.*;
public class Week07 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("資料筆數:");
int n = sc.nextInt();
int [] array_test = new int[n]; //依據使用者需求宣告陣列大小
double sum=0,avg=0;
double max=0,min=0;
for (int i=0; i<n; i++) {
System.out.printf("請輸入第%d筆資料",i+1);
array_test[i] = sc.nextInt();
sum += array_test[i]; //將使用者輸入的成績加總至sum變數中
if(i==0)
max = array_test[i]; //直接將第一筆資料設定為最高分
else{ //第二筆以後的資料
if(array_test[i]>max) //如果該筆資料>max
max = array_test[i]; //該筆設為max
}
if(i==0)
min = array_test[i]; //直接將第一筆資料設定為最低分
else{ //第二筆以後的資料
if(array_test[i]<min) //如果該筆資料<min
min = array_test[i]; //該筆設為min
}
}
avg=sum*1.0/n; //跳出迴圈後就可以直接計算平均成績
for (int i=0; i<n; i++)
//System.out.println("array["+i+"]="+array_test[i]); //the same
System.out.printf("array[%d]=%d\n",i,array_test[i]);
System.out.println("平均成績="+avg); //平均、高、低
System.out.println("最高成績="+max);
System.out.println("最低成績="+min);
}
}
Output:
資料筆數: [DrJava Input Box = 5]
請輸入第1筆資料 [DrJava Input Box = 64]
請輸入第2筆資料 [DrJava Input Box = 32]
請輸入第3筆資料 [DrJava Input Box = 100]
請輸入第4筆資料 [DrJava Input Box = 83]
請輸入第5筆資料 [DrJava Input Box = 94]
array[0]=64
array[1]=32
array[2]=100
array[3]=83
array[4]=94
平均成績=74.6
最高成績=100.0
最低成績=32.0
```
```java=
#簡化
import java.util.*;
public class Week07 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("資料筆數:");
int n = sc.nextInt();
int [] array_test = new int[n]; //依據使用者需求宣告陣列大小
double sum=0,avg=0;
double max=0,min=0;
for (int i=0; i<n; i++) {
System.out.printf("請輸入第%d筆資料",i+1);
array_test[i] = sc.nextInt();
sum += array_test[i]; //將使用者輸入的成績加總至sum變數中
if(i==0) {
max = array_test[i]; //直接將第一筆資料設定為最高分
min = array_test[i]; //直接將第一筆資料設定為最低分
}
else { //第二筆以後的資料
if(array_test[i]>max) //如果該筆資料>max
max = array_test[i]; //該筆設為max
if(array_test[i]<min) //如果該筆資料<min
min = array_test[i]; //該筆設為min
}
}
avg=sum*1.0/n; //跳出迴圈後就可以直接計算平均成績
for (int i=0; i<n; i++)
//System.out.println("array["+i+"]="+array_test[i]); //the same
System.out.printf("array[%d]=%d\n",i,array_test[i]);
System.out.println("平均成績="+avg); //平均、高、低
System.out.println("最高成績="+max);
System.out.println("最低成績="+min);
}
}
```
### 二維陣列
* int [] array_test = new int[n]; //依據使用者需求宣告陣列大小
```java=
//n個學生的3個成績,成績單格式。數統、JAVA、會計
//平均、min、max
import java.util.*;
public class Week07 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int [] sum = {0,0,0}; //用來記錄加總結果
double [] avg = {0,0,0}; //用來計算平均成績
int [] max = {0,0,0}; //用來記錄最高分
int [] min = {0,0,0}; //用來記錄最低分
System.out.println("資料筆數:");
int n = sc.nextInt();
int [][] array_test = new int[n][3]; ////依據使用者需求宣告陣列大小 //橫向3(科目),直向n(學生) //array[0][0]、array[0][1]、array[0][2]
int i,j; //迴圈的旗標變數
for (j=0; j<3; j++) { //先依科目別輸入成績
for (i=0; i<n; i++) { //醫學生流水號輸入成績
System.out.printf("請輸入第%d科第%d筆資料:",j+1,i+1);
array_test[i][j] = sc.nextInt();
sum[j] += array_test[i][j]; //將使用者輸入的成績加總至sum變數中
}
if(i==0) {
max[j] = array_test[i][j]; //直接將第一筆資料設定為最高分
min[j] = array_test[i][j]; //直接將第一筆資料設定為最低分
}
else { //若是第二筆以後的資料
if(array_test[i][j]>max[j]) //如果該筆資料>目前最高分
max[j] = array_test[i][j]; //該筆設為目前最高分
if(array_test[i][j]<min[j]) //如果該筆資料<目前最低分
min[j] = array_test[i][j]; //該筆設為目前最低分
}
}
for (j=0; i<3; j++){
avg[j]=sum[j]*1.0/n; //跳出迴圈後就可以直接計算平均成績
System.out.printf("第%d科平均成績=%f\n",j+1,avg[j]);
System.out.printf("第%d科最高成績=%d\n"+max);
System.out.printf("第%d科最低成績=%d\n"+min);
}
//System.out.println("array["+i+"]="+array_test[i]); //the same
}
}
```
## HW: 二維陣列-成績表
```java=
import java.util.*;
public class Week07 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int [] sum = {0,0,0}; //用來記錄加總結果
double [] avg = {0,0,0}; //用來計算平均成績
int [] max = {0,0,0};
int [] min = {0,0,0}; //用來記錄最高分及最低分
double [] avg_row = {0,0,0}; //用來計算表格內橫向的平均成績
System.out.println("please enter 資料筆數:");
int n = sc.nextInt();
int [][] array = new int [n][3]; //依據使用者需求來宣告陣列大小
int i,j; //用於迴圈的旗標變數
int [] sum_row = new int [n];
for (j=0; j<3; j++) { //先依科目別輸入成績
for (i=0; i<n; i++) { //依學生流水號輸入成績
System.out.printf("請輸入第%d科第%d筆資料:", j+1, i+1);
array[i][j] = sc.nextInt();
sum[j] += array[i][j]; //將使用者輸入的成績加總至sum變數中 (某一科目的全班平均)
sum_row[i] += array[i][j]; //將使用者輸入的成績加總至sum變數中 (某一學生的三科平均)
if (i==0) {
max[j] = array [i][j]; //直接將第一筆資料設定為最高分
min[j] = array [i][j]; //直接將第一筆資料設定為最低分
}
else { //若是第二筆以後的資料
if (array[i][j] > max[j]) //如果該筆資料>目前最高分
max[j] = array[i][j]; //將該資料設為目前最高分
if (array[i][j] < min[j]) //如果該筆資料<目前最低分
min[j] = array[i][j]; //將該資料設為目前最低分
}
}
}
System.out.println(" # 數統 JAVA 會計 平均 ");
System.out.println("============================");
for(i=0; i<n; i++)
System.out.printf("%2d %4d %4d %4d %2f \n", i+1, array[i][0], array[i][1], array[i][2], sum_row[i]*1.0/3); //sum_row[i]*1.0/3 為計算該學生三科的平均
System.out.println("============================");
for(j=0; j<3; j++) { //不同科目在迴圈內依序處理
avg[j] = sum[j]*1.0/n; //在列印前依據sum陣列內的總分計算出平均
System.out.printf("第%d科平均成績=%f\n", j+1, avg[j]);
System.out.printf("第%d科最高分=%d\n", j+1, max[j]);
System.out.printf("第%d科最低分=%d\n", j+1, min[j]);
}
}
}
```
# 2018-10-29 class
## 高鐵自由座(全票、優待票價錢 + 上下車站)
```java=
#方法一
import java.util.*;
public class Week08 {
public static void main(String[] args) {
int [][] fare = {{0,15,30,95,160,230,360,420,470,540,670,740},{35,0,15,75,140,205,335,395,450,520,650,720},{65,35,0,60,125,190,320,380,435,505,640,705},{190,155,125,0,60,135,260,320,375,445,575,645},
{320,280,250,125,0,65,195,260,310,380,510,580},{465,415,385,270,135,0,130,185,240,310,445,510},{725,675,645,520,395,260,0,60,110,180,315,380},{840,795,765,645,520,375,125,0,50,120,255,320},
{940,900,870,755,620,485,220,105,0,70,200,270},{1085,1045,1015,890,765,620,365,240,145,0,135,195},{1345,1305,1280,1150,1025,890,630,510,405,270,0,65},{1480,1445,1415,1290,1160,1025,765,645,540,395,135,0}};
Scanner sc = new Scanner(System.in);
System.out.println("請輸入 全票-0,優待票-1: ");
int type = sc.nextInt();
System.out.println("上車的編號: ");
int start = sc.nextInt();
System.out.println("下車的編號: ");
int stop = sc.nextInt();
//System.out.println("fare[0][2] = " + fare[0][2]); //print(30)
//System.out.println("fare[3][0] = " + fare[3][0]); //print(190)
int max = fare[start][stop];
int min = fare[stop][start];
if (max < min){
max = fare[stop][start];
min = fare[start][stop];
}
if (type==0){
System.out.println("票價:" + max);
}else{
System.out.println("票價:" + min);
}
}
}
```
```java=
#方法二
int answer = 0;
if (type==0 && start < stop){
answer = fare[stop][start];
}else if(type==1 && start > stop){
answer = fare[stop][start];
}else{
answer = fare[start][stop];
}
System.out.println("票價:" + answer);
```
## HW:(自由座+對號座)
```java=
import java.util.*;
public class Week08 {
public static void main(String[] args) {
int [][][] fare = {{{0,15,30,95,160,230,360,420,470,540,670,740},{35,0,15,75,140,205,335,395,450,520,650,720},{65,35,0,60,125,190,320,380,435,505,640,705},{190,155,125,0,60,135,260,320,375,445,575,645},
{320,280,250,125,0,65,195,260,310,380,510,580},{465,415,385,270,135,0,130,185,240,310,445,510},{725,675,645,520,395,260,0,60,110,180,315,380},{840,795,765,645,520,375,125,0,50,120,255,320},
{940,900,870,755,620,485,220,105,0,70,200,270},{1085,1045,1015,890,765,620,365,240,145,0,135,195},{1345,1305,1280,1150,1025,890,630,510,405,270,0,65},{1480,1445,1415,1290,1160,1025,765,645,540,395,135,0}},
{{0,20,35,100,165,240,375,435,485,560,695,765},{40,0,20,80,145,215,350,410,465,540,675,745},{70,40,0,65,130,200,335,395,450,525,660,730},{200,160,130,0,65,140,270,335,390,460,595,665},
{330,290,260,130,0,70,205,270,320,395,530,600},{480,430,400,280,140,0,135,195,250,320,460,530},{750,700,670,540,410,270,0,65,115,190,325,395},{870,820,790,670,540,390,130,0,55,125,265,335},
{970,930,900,780,640,500,230,110,0,75,210,280},{1120,1080,1050,920,790,640,380,250,150,0,140,205},{1390,1350,1320,1190,1060,920,650,530,420,280,0,70},{1530,1490,1460,1330,1200,1060,790,670,560,410,140,0}}}; //使用三維陣列
Scanner sc = new Scanner(System.in);
System.out.println("請輸入 自由座-0,對號座-1: ");
int seat_type = sc.nextInt();
System.out.println("請輸入 全票-0,優待票-1: ");
int type = sc.nextInt();
System.out.println("上車的編號: ");
int start = sc.nextInt();
System.out.println("下車的編號: ");
int stop = sc.nextInt();
int max = fare[seat_type][start][stop]; //預設最大值為fare[seat_type][start][stop]
int min = fare[seat_type][stop][start];
if (max < min){ //比大小,max為全票,min為優待票
max = fare[seat_type][stop][start];
min = fare[seat_type][start][stop];
}
if (type==0){
System.out.println("票價:" + max);
}else{
System.out.println("票價:" + min);
}
}
}
```