import java.io.*;
public class Main {
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
// 5人分の点数
int[] scores = new int[5];
for (int i = 0; i < scores.length; i++) {
// 点数を入力
System.out.print((i + 1) + "番目の人の点数を入力してください:");
scores[i] = Integer.parseInt(br.readLine());
}
// 入力結果を表示する
System.out.print("入力された点数は ");
for (int i = 0; i < scores.length; i++) {
System.out.print(scores[i] + " ");
}
System.out.println("です。");
}
}
実行結果
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
// 5人分の点数
int[] scores = new int[5];
// 点数をコンマ区切りで入力させる
System.out.println("5人の点数をコンマ区切りで入力してください。");
String str = br.readLine();
// 入力された文字列をコンマで分割し、int型に変換して配列に格納する
String[] strArray = str.split(",");
for (int i = 0; i < strArray.length; i++){
scores[i] = Integer.parseInt(strArray[i]);
}
// 入力結果を表示する
System.out.print("入力された点数は ");
for (int i = 0; i < scores.length; i++) {
System.out.print(scores[i] + " ");
}
System.out.println("です。");
}
}
実行結果
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException{
// 10人分の点数
int[] scores = new int[10];
// コンマ区切りの点数をファイルから読み込む
System.out.println("10人分の点数をファイルから読み込みます。");
BufferedReader br = new BufferedReader(new FileReader("scores.txt"));
String str = br.readLine();
// 入力された文字列をコンマで分割し、int型に変換して配列に格納する
String[] strArray = str.split(",");
for (int i = 0; i < strArray.length; i++){
scores[i] = Integer.parseInt(strArray[i]);
}
// 入力結果を表示する
System.out.print("読み込んだ点数は ");
for (int i = 0; i < scores.length; i++) {
System.out.print(scores[i] + " ");
}
System.out.println("です。");
}
}
scores.txt
実行結果
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException{
// 10人分の点数
int[] scores = new int[10];
// コンマ区切りの点数をファイルから読み込む
System.out.println("10人分の点数をファイルから読み込みます。");
BufferedReader br = new BufferedReader(new FileReader("scores.txt"));
String str = br.readLine();
// 入力された文字列をコンマで分割し、int型に変換して配列に格納する
String[] strArray = str.split(",");
for (int i = 0; i < strArray.length; i++){
scores[i] = Integer.parseInt(strArray[i]);
}
// 入力結果を表示する
System.out.print("読み込んだ点数は ");
for (int i = 0; i < scores.length; i++) {
System.out.print(scores[i] + " ");
}
System.out.println("です。");
// 平均点を計算する
float sum = 0;
for (int i = 0; i < scores.length; i++) {
sum += scores[i];
}
float average = sum / scores.length;
// 分散を計算する
float variance = 0;
for (int i = 0; i < scores.length; i++) {
variance += (scores[i] - average) * (scores[i] - average);
}
variance /= scores.length;
// 標準偏差を計算する
float standardDeviation = (float) Math.sqrt(variance);
// 結果を小数第1位まで丸めて表示する
System.out.println("平均点は " + Math.round(average * 10) / 10.0 + " 点です。");
System.out.println("分散は " + Math.round(variance * 10) / 10.0 + " です。");
System.out.println("標準偏差は " + Math.round(standardDeviation * 10) / 10.0 + " です。");
}
}
実行結果
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException{
// 10人分の点数
int[] scores = new int[10];
// コンマ区切りの点数をファイルから読み込む
System.out.println("10人分の点数をファイルから読み込みます。");
BufferedReader br = new BufferedReader(new FileReader("scores.txt"));
String str = br.readLine();
br.close();
// 入力された文字列をコンマで分割し、int型に変換して配列に格納する
String[] strArray = str.split(",");
for (int i = 0; i < strArray.length; i++){
scores[i] = Integer.parseInt(strArray[i]);
}
// 入力結果を表示する
System.out.print("読み込んだ点数は ");
for (int i = 0; i < scores.length; i++) {
System.out.print(scores[i] + " ");
}
System.out.println("です。");
// 平均点を計算する
float sum = 0;
for (int i = 0; i < scores.length; i++) {
sum += scores[i];
}
float average = sum / scores.length;
// 分散を計算する
float variance = 0;
for (int i = 0; i < scores.length; i++) {
variance += (scores[i] - average) * (scores[i] - average);
}
variance /= scores.length;
// 標準偏差を計算する
float standardDeviation = (float) Math.sqrt(variance);
// 結果を小数第1位まで丸めて表示する
System.out.println("平均点は " + Math.round(average * 10) / 10.0 + " 点です。");
System.out.println("分散は " + Math.round(variance * 10) / 10.0 + " です。");
System.out.println("標準偏差は " + Math.round(standardDeviation * 10) / 10.0 + " です。");
// 計算した情報をファイルに書き出す
BufferedWriter bw = new BufferedWriter(new FileWriter("result.txt"));
bw.write("平均点は " + Math.round(average * 10) / 10.0 + " 点です。");
bw.newLine();
bw.write("分散は " + Math.round(variance * 10) / 10.0 + " です。");
bw.newLine();
bw.write("標準偏差は " + Math.round(standardDeviation * 10) / 10.0 + " です。");
bw.close();
}
}
result.txt
実行結果