Created
December 8, 2016 12:01
-
-
Save mokomokohitsuzi/022b79f0c20537a825a8dab0f46d9df5 to your computer and use it in GitHub Desktop.
新・明解Java入門 演習6-8
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_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