From 9126cff44036ccf2272b12e8c956dbf93732b9ad Mon Sep 17 00:00:00 2001 From: t-eguchi <26-t-eguchi-tmn@it-college.co.jp> Date: Fri, 11 Sep 2026 15:40:48 +0900 Subject: [PATCH] =?UTF-8?q?Q4=E7=B5=82=E3=82=8F=E3=82=8A=E3=81=BE=E3=81=97?= =?UTF-8?q?=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Question04-while/src/PTra04_01.java | 5 ++++- Question04-while/src/PTra04_02.java | 6 ++++++ Question04-while/src/PTra04_03.java | 6 ++++++ Question04-while/src/PTra04_04.java | 12 ++++++++++- Question04-while/src/PTra04_05.java | 31 ++++++++++++++++++++++++++--- 5 files changed, 55 insertions(+), 5 deletions(-) diff --git a/Question04-while/src/PTra04_01.java b/Question04-while/src/PTra04_01.java index 08c2624..f1a47b4 100644 --- a/Question04-while/src/PTra04_01.java +++ b/Question04-while/src/PTra04_01.java @@ -15,9 +15,12 @@ public class PTra04_01 { // 以下のwhile文は0~9のランダムな値をコンソールに出力する処理を無限に繰り返します。 // TODO while文の条件を修正して5が出力されたら繰り返しを止めるようにしてください。 - while(true) { + while(randomNumber != 5) { // 0~9のランダムな値を生成 randomNumber = random.nextInt(10); + + + // 生成した値をコンソールに出力 System.out.println(randomNumber); } diff --git a/Question04-while/src/PTra04_02.java b/Question04-while/src/PTra04_02.java index 4ce7b65..2def9ea 100644 --- a/Question04-while/src/PTra04_02.java +++ b/Question04-while/src/PTra04_02.java @@ -15,6 +15,12 @@ public class PTra04_02 { * 繰り返しの中でnumを2倍した数値をnumに代入してください。 * ※ 繰り返しにはwhile文を用いてください */ + while (num <= 1000) { + num = num * 2; + System.out.println(num); + } + + } } diff --git a/Question04-while/src/PTra04_03.java b/Question04-while/src/PTra04_03.java index 21b7f8e..f7722fe 100644 --- a/Question04-while/src/PTra04_03.java +++ b/Question04-while/src/PTra04_03.java @@ -19,8 +19,14 @@ public class PTra04_03 { while(true) { // 0~9のランダムな値を生成 randomNumber = random.nextInt(10); + if (randomNumber == 5) { + System.out.println("5が出たので終了します。"); + break; + + } // 生成した値をコンソールに出力 System.out.println(randomNumber); + } } diff --git a/Question04-while/src/PTra04_04.java b/Question04-while/src/PTra04_04.java index ecaa1fe..5b73c9f 100644 --- a/Question04-while/src/PTra04_04.java +++ b/Question04-while/src/PTra04_04.java @@ -18,13 +18,15 @@ public class PTra04_04 { Random random = new Random(); // TODO 双六の総コマ数としてint型の変数mapを宣言し、50を代入してください。 + int map = 50; // TODO 現在までの進んだコマ数としてint型の変数progressを宣言し、0を代入してください。 + int progress = 0; System.out.println("===== START ====="); // TODO 変数mapとprogressを用いて、ゴールするまで繰り返すようにwhile文の条件を記述してください。 - while() { + while(progress < map) { // 入力を促すメッセージを出力し、エンターキーが押されるのを待ちます。 System.out.println("Enterキーを押してください。さいころをふります。"); scanner.nextLine(); @@ -33,12 +35,20 @@ public class PTra04_04 { int dice = random.nextInt(6) + 1; // TODO 変数diceを用いて、"#の目が出ました。"というメッセージを出力してください。 + System.out.println(dice + "の目が出ました"); // ※ #には変数diceの値が入ります。 // TODO 変数progressに変数diceの値を加算してください。 + progress = progress + dice; // TODO まだゴールしていない場合は"残りのマスは#マスです。"というメッセージを出力してください。 // ※ #には双六の残りのマス数が入ります。 + if(progress < map) { + System.out.println("残りのマスは" + (map - progress) + "マスです"); + + } + + } diff --git a/Question04-while/src/PTra04_05.java b/Question04-while/src/PTra04_05.java index 2b15402..85deab2 100644 --- a/Question04-while/src/PTra04_05.java +++ b/Question04-while/src/PTra04_05.java @@ -1,3 +1,4 @@ + /* * PTra04_05.java * 作成 Rhizome @@ -21,7 +22,7 @@ public class PTra04_05 { boolean winFlag = false; // プレイヤーが勝つまでじゃんけんを繰り返す。 - while(!winFlag) { + while (!winFlag) { int playerHand; int npcHand; @@ -32,14 +33,38 @@ public class PTra04_05 { System.out.println("0:グー, 1:チョキ, 2:パー"); playerHand = Integer.parseInt(scanner.nextLine()); // TODO 選ばれた手を出力してください(0なら"あなたはグーを出しました"等) - + if (playerHand == 0) { + System.out.println("あなたはグーを出しました"); + } else if (playerHand == 1) { + System.out.println("あなたはチョキをだしました"); + } else if (playerHand == 2) { + System.out.println("あなたはパーを出しました"); + } else { + System.out.println("正しい数字を記入してください"); + continue; + } // 対戦相手の手をランダムに選択する。 npcHand = random.nextInt(3); // TODO 対戦相手の手を出力してください(0なら"相手はグーを出しました"等) - + if (npcHand == 0) { + System.out.println("相手はグーを出しました"); + } else if (npcHand == 1) { + System.out.println("相手はチョキをだしました"); + } else { + System.out.println("相手はパーを出しました"); + } // TODO じゃんけんの結果を出力し、プレイヤーが勝った場合はプログラムが終了するようにしてください。 + if (playerHand == npcHand) { + System.out.println("あいこです。もう一度!"); + } else if ((playerHand == 0 && npcHand == 2) || (playerHand == 1 && npcHand == 0) + || (playerHand == 2 && npcHand == 1)) { + System.out.println("あなたの負けです。もう一度!"); + } else { + System.out.println("あなたの勝ちです!"); + break; + } // 次のじゃんけんの前に改行しておく System.out.println(); }