Java で for ループを使用する方法についてヘルプが必要です
これはクラスの課題なので、答えを教えてもらうよりも、正しい方向に向けてもらいたいだけです。
「有効な 7 人のこびとのリスト: Sleepy、Bashful、Doc、Sneezy、Happy、Grumpy、Dopey ランダムな文字のプール、無効なこびと: Lion-O、Cheetara、Pantro、Tigra、Snarf、Donald Duck、Mickey Mouse、Minnie Mouse 、グーフィー、ヒースクリフ、ヒューイ、デューイ、ルーイ、スクルージ・マクダック、
次の変数を宣言します。
int counter = 7;
boolean firstSelection = false;
boolean secondSelection = false;
boolean thirdSelection = false;
boolean fourthSelection = false;
boolean fiveSelection = false;
boolean sixSelection = false;
boolean sevenSelection = false;
3 つの選択肢のリストをコンソールに出力します。3 つの選択肢から正しいドワーフを選択するようにユーザーに依頼します。3 つの選択肢のリストには、ランダムな文字リストからの 2 つの名前と、7 人の小人からの 1 つの名前が含まれます。選択肢の選択を処理するための switch ステートメントを作成します。間違ったケースが選択された場合は、counter という名前の int 変数をデクリメントし、コンソールに「間違った選択」を出力します。正しいケースが選択された場合は、対応するブール変数を true に変更します (つまり、. . firstSelection、secondSelection など) をコンソールに出力し、「こんにちは、あなたは正しいものを選びました」と出力します。デフォルトのケースでは、コンソールに「無効な選択」と出力されます。小人。
- for ループを使用する
- do-while ループを使用してループを再作成します。
- while ループを使用して再度ループを作成します。最後に、if-else ステートメントを作成します。このステートメントには、すべてのブール変数をテストする短絡 && があります。true の場合は、コンソールに「金の星を獲得しました!」というステートメントを出力します。それ以外の場合は、コンソールに「すべて正しくありませんでした」というステートメントを出力します。"
前回の課題、ループ抜きのこの仕様だけで問題なく完了しました。しかし、教授がループを問題にどのように統合することを望んでいるのか、私には本当にわかりません。私が考えることができる唯一のことは、彼がループを7回作成して、7回ごとに異なるドワーフについて尋ねるということです。それは可能ですか?実行中にループの内容を変更できますか? 私はこれについて正しく考えさえしていないように感じます。
前の割り当てからの私のコードは次のとおりです。sans loops:
import java.util.Scanner;
public class SevenDwarfs {
public static void main(String[] args) {
int counter = 7;
boolean firstSelection = false;
boolean secondSelection = false;
boolean thirdSelection = false;
boolean fourthSelection = false;
boolean fiveSelection = false;
boolean sixSelection = false;
boolean sevenSelection = false;
System.out
.println("Which of the following is one of the seven drawfs?");
System.out.println("1 Sleepy");
System.out.println("2 Lion-O");
System.out.println("3 Cheetara");
Scanner input = new Scanner(System.in);
int choice = input.nextInt();
switch (choice) {
case 1:
System.out.println("Hi Ho, you picked the correct one");
firstSelection = true;
break;
case 2:
System.out.println("Wrong selection");
--counter;
break;
case 3:
System.out.println("Wrong selection");
--counter;
break;
default:
System.out.println("Invalid selection");
--counter;
break;
}
System.out
.println("Which of the following is one of the seven drawfs?");
System.out.println("1 Panthro");
System.out.println("2 Bashful");
System.out.println("3 Tigra");
Scanner input2 = new Scanner(System.in);
int choice2 = input2.nextInt();
switch (choice2) {
case 1:
System.out.println("Wrong selection");
--counter;
break;
case 2:
System.out.println("Hi Ho, you picked the correct one");
secondSelection = true;
break;
case 3:
System.out.println("Wrong selection");
--counter;
break;
default:
System.out.println("Invalid selection");
--counter;
break;
}
System.out
.println("Which of the following is one of the seven drawfs?");
System.out.println("1 Snaf");
System.out.println("2 Doc");
System.out.println("3 Donald Duck");
Scanner input3 = new Scanner(System.in);
int choice3 = input3.nextInt();
switch (choice3) {
case 1:
System.out.println("Wrong selection");
--counter;
break;
case 2:
System.out.println("Hi Ho, you picked the correct one");
thirdSelection = true;
break;
case 3:
System.out.println("Wrong selection");
--counter;
break;
default:
System.out.println("Invalid selection");
--counter;
break;
}
System.out
.println("Which of the following is one of the seven drawfs?");
System.out.println("1 Mickie Mouse");
System.out.println("2 Sneezy");
System.out.println("3 Minie Mouse");
Scanner input4 = new Scanner(System.in);
int choice4 = input4.nextInt();
switch (choice4) {
case 1:
System.out.println("Wrong selection");
--counter;
break;
case 2:
System.out.println("Hi Ho, you picked the correct one");
fourthSelection = true;
break;
case 3:
System.out.println("Wrong selection");
--counter;
break;
default:
System.out.println("Invalid selection");
--counter;
break;
}
System.out
.println("Which of the following is one of the seven drawfs?");
System.out.println("1 Heathcliff");
System.out.println("2 Happy");
System.out.println("3 Goofy");
Scanner input5 = new Scanner(System.in);
int choice5 = input5.nextInt();
switch (choice5) {
case 1:
System.out.println("Wrong selection");
--counter;
break;
case 2:
System.out.println("Hi Ho, you picked the correct one");
fiveSelection = true;
break;
case 3:
System.out.println("Wrong selection");
--counter;
break;
default:
System.out.println("Invalid selection");
--counter;
break;
}
System.out
.println("Which of the following is one of the seven drawfs?");
System.out.println("1 Huey");
System.out.println("2 Grumpy");
System.out.println("3 Dewey");
Scanner input6 = new Scanner(System.in);
int choice6 = input6.nextInt();
switch (choice6) {
case 1:
System.out.println("Wrong selection");
--counter;
break;
case 2:
System.out.println("Hi Ho, you picked the correct one");
sixSelection = true;
break;
case 3:
System.out.println("Wrong selection");
--counter;
break;
default:
System.out.println("Invalid selection");
--counter;
break;
}
System.out
.println("Which of the following is one of the seven drawfs?");
System.out.println("1 Scrooge McDuck");
System.out.println("2 Dopey");
System.out.println("3 Louie");
Scanner input7 = new Scanner(System.in);
int choice7 = input7.nextInt();
switch (choice7) {
case 1:
System.out.println("Wrong selection");
--counter;
break;
case 2:
System.out.println("Hi Ho, you picked the correct one");
sevenSelection = true;
break;
case 3:
System.out.println("Wrong selection");
--counter;
break;
default:
System.out.println("Invalid selection");
--counter;
break;
}
if (firstSelection == true && secondSelection == true
&& thirdSelection == true && fourthSelection == true
&& fiveSelection == true && sixSelection == true
&& sevenSelection == true) {
System.out.println("You earned a gold star!");
} else {
System.out.println("\nYou did not get all correct.");
}
}
}