Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement