Skip to content

Instantly share code, notes, and snippets.

@mokomokohitsuzi
Created December 8, 2016 12:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mokomokohitsuzi/022b79f0c20537a825a8dab0f46d9df5 to your computer and use it in GitHub Desktop.
Save mokomokohitsuzi/022b79f0c20537a825a8dab0f46d9df5 to your computer and use it in GitHub Desktop.
新・明解Java入門 演習6-8
import java.util.Scanner;
public class En06_08 {
public static void main(String[] args) {
Scanner stdIn = new Scanner(System.in);
int youso;
do {
System.out.print("要素数:");
youso = stdIn.nextInt();
} while (youso < 1);
// 配列を作成
double[] doubles = new double[youso];
// for文で値を配列へ代入する
for (int i = 0; i < doubles.length; i++) {
System.out.printf("要素a[%d]:", i);
doubles[i] = stdIn.nextDouble();
}
System.out.println();
// 合計を計算
double sum = 0;
for (double i : doubles) {
sum += i;
}
System.out.printf("合計:%.1f\n", sum);
System.out.printf("平均:%.1f\n", (sum / youso));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment