Last active
December 15, 2016 13:02
-
-
Save mokomokohitsuzi/ecd18a7b8a3e3b2d16149fa87ef1e734 to your computer and use it in GitHub Desktop.
新・明解Java入門 演習6-15
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Random; | |
import java.util.Scanner; | |
public class En06_15 { | |
public static void main(String[] args) { | |
Random rand = new Random(); | |
Scanner stdIn = new Scanner(System.in); | |
// 曜日配列を作成 | |
String[] weeks = { "sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday" }; | |
String[] weeksj = { "日", "月", "火", "水", "木", "金", "土" }; | |
// 問題を表示。 | |
System.out.println("英語の曜日名を小文字で入力して下さい。"); | |
// 1つ前の問題が出ないようにする。(最初の問題の過去問は0~11以外の数字を入れておく) | |
int ransu; | |
int exam0 = -1; | |
Roop: while (true) { | |
do { | |
// 問題用乱数の作成 | |
ransu = rand.nextInt(7); | |
} while (ransu == exam0); | |
// 過去問に乱数を代入。 | |
exam0 = ransu; | |
// ループ用ラベル1 | |
Retry: while (true) { | |
System.out.printf("%s曜日:", weeksj[ransu]); | |
// 文字列を入力させる。 | |
String answerWeek = stdIn.next(); | |
// 文字列の比較を実施 | |
if (weeks[ransu].equals(answerWeek)) { | |
System.out.print("正解です。もう一度? 1…Yes/0…No:"); | |
int roop = stdIn.nextInt(); | |
if (roop == 0) { | |
// 終了する場合は、Roopを抜ける | |
break Roop; | |
} else { | |
// 最初に戻る | |
continue Roop; | |
} | |
} else { | |
System.out.println("違います"); | |
// Retryラベルまで戻る | |
continue Retry; | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment