Advertisement
Guest User

Untitled

a guest
Dec 8th, 2016
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.67 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class En06_08 {
  4. public static void main(String[] args) {
  5. Scanner stdIn = new Scanner(System.in);
  6.  
  7. int youso;
  8. do {
  9. System.out.print("要素数:");
  10. youso = stdIn.nextInt();
  11. } while (youso < 1);
  12.  
  13. // 配列を作成
  14. double[] doubles = new double[youso];
  15.  
  16. // for文で値を配列へ代入する
  17. for (int i = 0; i < doubles.length; i++) {
  18. System.out.printf("要素a[%d]:", i);
  19. doubles[i] = stdIn.nextDouble();
  20. }
  21. System.out.println();
  22.  
  23. // 合計を計算
  24. double sum = 0;
  25. for (double i : doubles) {
  26. sum += i;
  27. }
  28.  
  29. System.out.printf("合計:%.1f\n", sum);
  30. System.out.printf("平均:%.1f\n", (sum / youso));
  31. }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement