エンジニア成長日記 swaponQ

コンピュータサイエンス専攻の一般人のブログです。

新・明解 Java 入門 演習 6-5, 6-6, 6-7, 6-8 解答

Hello, Terminal!swaponQです!

今回は演習 6-5, 6-6, 6-7, 6-8 に取り組んでいこうと思います。

  • 演習 6-5

配列の要素数と、個々の要素の値を読み込んで、各要素の値を表示するプログラムを作成せよ。表示の形式は、初期化子を同じ形式、すなわち、各要素の値をコンマで区切って{}で囲んだ形式とすること。


ソースコード

import java.util.Scanner;

class ex06_5 {
	public static void main(String[] args) {
		Scanner stdIn = new Scanner(System.in);
		System.out.print("要素数:");
		int a = stdIn.nextInt();
		int[] array = new int[a];
		for (int i = 0; i < array.length; i++) {
			System.out.printf("a[%d]:",i);
				array[i]=stdIn.nextInt();
		}
		//for文で結果を{}の中に数字で表示
		System.out.print("a = {");
		for(int i = 0; i < array.length; i++){
			System.out.print(array[i]);
			System.out.print((i + 1 == array.length)?"":", ");
		}
		System.out.println("}");
	}
}

・実行結果

要素数:3
a[0]:5
a[1]:7
a[2]:8
a = {5, 7, 8}
  • 演習 6-6

テストの点数の合計点、平均点、最高点、最低点を表示するプログラムを作成せよ。人数と点数はキーボードから読み込むこと。

ソースコード

import java.util.Scanner;

class ex06_6 {
	public static void main(String[] args) {
		Scanner stdIn = new Scanner(System.in);
		int n;
		do {
			System.out.print("人数:");
      n = stdIn.nextInt();
		} while (n <= 0);
		int score[] = new int[n];
		for (int i = 0; i < score.length; i++) {
			System.out.printf("%d人目の点数:", (i + 1));
			score[i] = stdIn.nextInt();
		}
		int max = score[0];
		int min = score[0];
		for (int i = 0; i < score.length; i++) {
			if (score[i] > max) {
				max = score[i];
			}
			if (min > score[i]) {
				min = score[i];
			}
		}
		System.out.println();
		int sum = 0;
		for (int i = 0; i < score.length; i++) {
			sum += score[i];
		}
    double ave = sum / (double) n;
		System.out.printf("最高点:%d\n", max);
		System.out.printf("最低点:%d\n", min);
		System.out.printf("合計点:%d\n", sum);
		System.out.printf("平均点:%.1f\n", ave);
	}
}

・実行結果

人数:5
1人目の点数:22
2人目の点数:5
3人目の点数:7
4人目の点数:4
5人目の点数:6

最高点:22
最低点:4
合計点:44
平均点:8.8
  • 演習 6-7

list6-9は探索するキー値と同じ値の要素が複数個存在する場合、最も先頭に位置する要素を見つけるプログラムである。最も末尾側に位置する要素を見つけるプログラムを作成せよ。

ソースコード

import java.util.Random;
import java.util.Scanner;

class ex06_7 {
	public static void main(String[] args) {
		Random rand = new Random();
		Scanner stdIn = new Scanner(System.in);
		final int n = 12;
		int[] a = new int[n];
		for (int j = 0; j < n; j++) {
			a[j] = rand.nextInt(10);
		}
		System.out.print("配列aの全要素の値\n{ ");
		for (int j = 0; j < n; j++) {
			System.out.print(a[j] + " ");
		}
		System.out.println("}");
		System.out.print("探す数値:");
		int key = stdIn.nextInt();
		int i;
		for (i = n - 1; i >= 0; i--) {
			if (a[i] == key) {
				break;
			}
		}
		if (i >= 0) {
			System.out.println("それはa[" + i + "]にあります。");
		} else {
			System.out.println("それはありません。");
		}
	}
}

・実行結果

配列aの全要素の値
{ 3 8 2 0 4 2 7 6 6 4 7 2 }
探す数値:7
それはa[10]にあります。
  • 演習 6-8

double型の配列の全要素の合計値と平均値を求めるプログラムを作成せよ。要素数と全要素の値はキーボードから読み込むこと。

ソースコード

import java.util.Scanner;

class ex06_8 {
	public static void main(String[] args) {
		Scanner stdIn = new Scanner(System.in);
		int n;
		do {
			System.out.print("要素数:");
			n = stdIn.nextInt();
		} while (n < 1);
		double[] array = new double[n];
		for (int i = 0; i < array.length; i++) {
			System.out.printf("要素a[%d]:", i);
			array[i] = stdIn.nextDouble();
		}
		System.out.println();
		double sum = 0;
		for (double i : array) {
			sum += i;
		}
    double ave = sum / n;
		System.out.printf("合計:%.1f\n", sum);
		System.out.printf("平均:%.1f\n", ave);
	}
}

・実行結果

要素数:5
要素a[0]:1
要素a[1]:7
要素a[2]:3
要素a[3]:6
要素a[4]:3

合計:20.0
平均:4.0

今回は以上です。お疲れ様でした!
次回は演習 6-9, 6-10, 6-11, 6-12 です。

Goodbye, Terminal… swaponQでした!