Last active
December 7, 2016 13:31
-
-
Save mokomokohitsuzi/caf5037d762036ded5f6af51b9b02deb to your computer and use it in GitHub Desktop.
新・明解Java入門 演習6-6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
public class En06_06 { | |
public static void main(String[] args) { | |
Scanner stdIn = new Scanner(System.in); | |
// 人数用変数 | |
int man; | |
do { | |
System.out.println("点数の計算をします。"); | |
System.out.print("何人計算しますか:"); | |
man = stdIn.nextInt(); | |
if (man <= 0) { | |
System.out.println("1人以上を入力して下さい。"); | |
} | |
} while (man <= 0); | |
// 配列用変数(最大値man) | |
int points[] = new int[man]; | |
// 配列を回しながら、点数の代入を行う | |
for (int i = 0; i < points.length; i++) { | |
System.out.printf("%d番目の点数:", (i + 1)); | |
points[i] = stdIn.nextInt(); | |
} | |
// 最大・最小値を求める(操作) | |
int max = points[0]; | |
int min = points[0]; | |
for (int i = 0; i < points.length; i++) { | |
// もし、point[i]よりも数値が大きいor小さい場合は、 | |
// その値を代入する。 | |
if (points[i] > max) { | |
max = points[i]; | |
} | |
if (min > points[i]) { | |
min = points[i]; | |
} | |
} | |
System.out.println(); | |
// 合計を求める | |
int sum = 0; | |
for (int i = 0; i < points.length; i++) { | |
sum += points[i]; | |
} | |
System.out.printf("最高点:%d\n", max); | |
System.out.printf("最低点:%d\n", min); | |
System.out.printf("合計点:%d\n", sum); | |
// 平均点を求める(manをdoubleでキャストする) | |
System.out.printf("平均点:%.1f\n", (sum / (double) man)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment