エンジニア成長日記 swaponQ

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

新・明解 Java 入門 演習 6-13, 6-14, 6-15 解答

Hello, Terminal!swaponQです!

今回は演習 6-13, 6-14, 6-15 に取り組んでいこうと思います。

  • 演習 6-13

配列aの全要素を配列bに対して逆順にコピーするプログラムを作成せよ。なお、二つの配列の要素数は同一であることを仮定して良い。

ソースコード

import java.util.Scanner;

class ex06_13 {
	public static void main(String[] args) {
		Scanner stdIn = new Scanner(System.in);
		System.out.print("要素数:");
		int n = stdIn.nextInt();
		int[] a = new int[n];
		int[] b = new int[n];
		for (int i = 0; i < n; i++) {
			System.out.printf("a[%d] =", i);
			a[i] = stdIn.nextInt();
		}
                System.out.println();
		for (int i = a.length, j = 0; i > 0; i--, j++) {
			b[j] = a[i - 1];
		}
		for (int i = 0; i < b.length; i++) {
			System.out.printf("b[%d] =%d\n", i, b[i]);
		}
	}
}

・実行結果

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

b[0] =3
b[1] =6
b[2] =4
b[3] =7
b[4] =3
  • 演習 6-14

月を1〜12の数値として表示して、その英語表現を入力させるプログラムを作成せよ。
・出題する月の値は乱数で生成する。
・学習者が望む限り、何度でも繰り返せる。
・同一月を連続して出題しない。
文字列s1とs2が等しいかどうかの判定は、s1.equals(s2)によって行える。

ソースコード

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

class ex06_14 {
	public static void main(String[] args) {
		Random rand = new Random();
		Scanner stdIn = new Scanner(System.in);
		String[] Month = { "January", "February", "March", "April", "May", "Jun", "July", "August", "September",
				"October", "November", "December" };
		System.out.println("英語の月名を入力して下さい。");
		System.out.println("なお、先頭は大文字で、2文字目以降は小文字とします。");
		int r;
		int q = -1;
		LOOP: while (true) {
			do {
				r = rand.nextInt(12);
			} while (r == q);
			q = r;
			RE: while (true) {
				System.out.printf("%d月:", q + 1);
				String ans = stdIn.next();
				if (Month[q].equals(ans)) {
					System.out.print("正解です。もう一度? 1…Yes/0…No:");
					int flag = stdIn.nextInt();
					if (flag == 0) {
						break LOOP;
					} else {
						continue LOOP;
					}
				} else {
					System.out.println("違います");
					continue RE;
				}
			}
		}
	}
}

・実行結果

英語の月名を入力して下さい。
なお、先頭は大文字で、2文字目以降は小文字とします。
12月:December
正解です。もう一度? 1…Yes/0…No:0
  • 演習 6-15

曜日を表示して。その英語表現を入力させるプログラムを作成せよ。
・出題する曜日は乱数で生成する。
・学習者が望む限り、何度でも繰り返せる。
・同一曜日を連続して出題しない。

ソースコード

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

class ex06_15 {
	public static void main(String[] args) {
		Random rand = new Random();
		Scanner stdIn = new Scanner(System.in);
		String[] day = { "sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday" };
		String[] dayJ = { "日", "月", "火", "水", "木", "金", "土" };
		System.out.println("英語の曜日名を小文字で入力して下さい。");
		int r;
		int q = -1;
		LOOP: while (true) {
			do {
				r = rand.nextInt(7);
			} while (r == q);
			q = r;
			RE: while (true) {
				System.out.printf("%s曜日:", dayJ[q]);
				String ans = stdIn.next();
				if (day[q].equals(ans)) {
					System.out.print("正解です。もう一度? 1…Yes/0…No:");
					int flag = stdIn.nextInt();
					if (flag == 0) {
						break LOOP;
					} else {
						continue LOOP;
					}
				} else {
					System.out.println("違います");
					continue RE;
				}
			}
		}
	}
}

・実行結果

英語の曜日名を小文字で入力して下さい。
日曜日:sunday
正解です。もう一度? 1…Yes/0…No:0

今回は以上です。お疲れ様でした!
次回は演習 6-16, 6-17, 6-18, 6-19 です。

Goodbye, Terminal… swaponQでした!