-2

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.");
        }
    }
}
4

1 に答える 1

4

概念について間違って考えている可能性があることに気づき、助けを求めたという事実は良いことです.

以下を読んで、Java のループに慣れてください。

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html

あなたの質問に答えると、はい、ループを実行しながら内容を変更できます。そのための変数です。プログラムの実行中にそれらの値を変更できます。このサンプルを見てください。変数 i は、ループの反復ごとに増加します。変数 outsideLoop もループ内で変化します。これで遊ぶと、理解し始めるでしょう。

class ForDemo
{
    public static void main(String[] args)
    {
        int outsideLoop = 0;
        for (int i = 1; i < 11; i++)
        {
            outsideLoop += i;
            System.out.println("Count is: " + i);
        }
        System.out.println("Outside loop is: " + outsideLoop);
    }
}

選択範囲の印刷、ユーザー入力の取得、およびユーザー入力の検証のプロセスの開始点ができました。そのチャンクを (ループで) 7 回繰り返します。

于 2013-01-22T21:21:40.833 に答える