---
# System prepended metadata

title: 2019-11-04 上課範例
tags: [Java計算機程式設計, 108-1]

---

# 2019-11-04 上課範例

## PT3_8

```java=
import java.util.Scanner;

public class PT3_8 {

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		System.out.println("請輸入一個整數n：");
		int n = in.nextInt();
		in.close();

//		if (n == 0)
//			System.out.println("F" + n + " = " + n);
//		else if (n == 1)
//			System.out.println("F" + n + " = " + n);

		int Fn_2 = 0;
		int Fn_1 = 1;
		int Fn = 0;
		if (n == 0 || n == 1)
			System.out.println("F" + n + " = " + n);
		else {

			for (int i = 2; i <= n; i++) {
				Fn = Fn_1 + Fn_2;
				Fn_2 = Fn_1;
				Fn_1 = Fn;
			}
			System.out.println("F" + n + " = " + Fn);
		}
	}
}
```

## PT3_9a

```java=
import java.util.Scanner;

public class PT3_9a {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		System.out.println("請輸入一個整數n：");
		int n = in.nextInt();
		in.close();

		for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= i; j++) {
				System.out.print("*");
			}
			System.out.println();
		}
	}
}
```

## PT3_9c

```java=
import java.util.Scanner;

public class PT3_9c {

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		System.out.println("請輸入一個整數n：");
		int n = in.nextInt();
		in.close();

		int count = 0;
		for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= i; j++) {
				System.out.print(++count + "\t");
			}
			System.out.println();
		}

	}

}
```

## PT3_9b

留給同學們練習。
Note: 在印 `*` 之前需要先印空白 ` ` ，三角型才有可能靠右對齊，空白的三角型結構與 PT3_9a 相同


## PT4_1
```java=
import java.util.Scanner;

public class PT4_1 {

	// ==================
	// 使用者
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		System.out.println("請輸入一個整數a：");
		int a = in.nextInt();
		System.out.println("請輸入一個整數b：");
		int b = in.nextInt();
		in.close();

		int max = PT4_1.max2(a, b);

		System.out.println("最大值為：" + max);
	}

	// =====================
	// 開發者
	// 找最大值
	public static int max2(int x, int y) {
//		int ret = 0;
//		if (x >= y)
//			ret = x;
//		else
//			ret = y;

		return (x >= y) ? x : y;
	}

}
```

###### tags: `Java計算機程式設計` `108-1`