forked from JustinSDK/JavaSE6Tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAverageInput.java
More file actions
27 lines (21 loc) · 786 Bytes
/
AverageInput.java
File metadata and controls
27 lines (21 loc) · 786 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import java.util.Scanner;
public class AverageInput {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("請輸入學生人數: ");
int length = scanner.nextInt();
float[] score = new float[length]; // 動態配置長度
for(int i = 0; i < score.length; i++) {
System.out.print("輸入分數:");
float input = scanner.nextFloat();
score[i] = input;
}
System.out.print("\n分數:");
float total = 0;
for(int i = 0; i < score.length; i++) {
total = total + score[i];
System.out.print(score[i] + " ");
}
System.out.printf("\n平均:%.2f", total / score.length);
}
}