---
# System prepended metadata

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

---

# 2019-10-14 上課範例

## PT2

### 範例一 (錯誤)
```java=
import java.util.Scanner;

public class PT2 {

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		System.out.println("請輸入三角型的底：");
		int base = in.nextInt();
		System.out.println("請輸入三角型的高：");
		int heigh = in.nextInt();
		in.close();

		double area = (base * heigh) / 2;

		System.out.println("三角型面積：" + area);
	}

}
```

### 範例二

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

public class PT2 {

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		System.out.println("請輸入三角型的底：");
		int base = in.nextInt();
		System.out.println("請輸入三角型的高：");
		int heigh = in.nextInt();
		in.close();

		double area = (base * heigh) / 2.0;

		System.out.println("三角型面積：" + area);
	}

}

```

### 範例三

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

public class PT2 {

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		System.out.println("請輸入三角型的底：");
		int base = in.nextInt();
		System.out.println("請輸入三角型的高：");
		int heigh = in.nextInt();
		in.close();

		double area = (double)(base * heigh) / 2;

		System.out.println("三角型面積：" + area);
	}

}

```

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