LoginSignup
6
6

More than 5 years have passed since last update.

Javaを用いた平均値の出し方 高校数学Ⅰ

Posted at
SampleCode

package Sample5;

import java.util.Scanner;

public class Sample39 {
    public static void main(String[] args){

        Scanner scan = new Scanner(System.in);

        System.out.println("平均値を求めます");
        System.out.println("データの個数を入力してください");

        int a = scan.nextInt();

        double[] num = new double[a]; // 入力した数値を要素の個数として定義する
        double sum = 0.0;
        for(int i = 0; i < num.length; i++){
            System.out.println((i + 1) +"個めのデータを入力してください");
            num[i] = scan.nextDouble();

        }

        for(int i = 0; i < num.length; i++){
            sum += num[i];  //入力された総和を求める

        }

        System.out.println("平均点は "+ (sum/num.length)+ " 点です");
        scan.close();
    }

}

データの個数は整数でそれ以外は小数も可です
※分数は未確認

6
6
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
6