0

コードは次のとおりです。

package classes;
import java.util.*;

public class Introduction {
    Scanner Input = new Scanner(System.in);
    int classChoose;
    boolean repeat = false;

    public void Introduction() {
        System.out.println("\t===THE QUEST FOR PERSEPOLIS===\tv 1.0\n");
        System.out.println("Please choose a class: ");
        System.out.print("(1)Elite Knight\t");
        System.out.print("(2)Dawnguard\n");
        System.out.print("(3)Archer\t\t\t");
        System.out.print("(4)Barbarian\n");
        System.out.print("(5)Mage\t\t\t");
        System.out.print("(6)Swordsman\n");
        System.out.println("(7)Crossbowman\t");

        do {
            try {
                repeat = false;
                classChoose = Input.nextInt();
                while(classChoose < 1 || classChoose > 7) {
                    repeat = false;
                    System.out.println("Error. Enter a number between 1 and 7(inclusive).");
                    classChoose = Input.nextInt();
                }
            }
            catch(InputMismatchException e) {
                repeat = true;
                System.err.println("Caught: "+e);
                Input.nextLine();
            }
        }while(repeat = true);

        switch(classChoose) {
            case 1: chooseKnight();
                    break;
            case 2: chooseGuard();
                    break;
            case 3: chooseArcher();
                    break;
            case 4: chooseBarbarian();
                    break;
            case 5: chooseMage();
                    break;
            case 6: chooseSwordsman();
                    break;
            case 7: chooseCrossbowman();
                    break;
        }
    }
    public static void chooseKnight() {
        System.out.println("You have chosen the Elite Knight. You will be briefed and then you shall be set "
        +"on your quest!");
    }
    static void chooseGuard() {
        System.out.println("You have chosen the Dawnguard. You will be briefed and then you shall be set "
        +"on your quest!");
    }
    static void chooseArcher() {
        System.out.println("You have chosen the Archer. You will be briefed and then you shall be set "
        +"on your quest!");
    }
    static void chooseBarbarian() {
        System.out.println("You have chosen the Barbarian. You will be briefed and then you shall be set "
        +"on your quest!");
    }
    static void chooseMage() {
        System.out.println("You have chosen the Mage. You will be briefed and then you shall be set "
        +"on your quest!");
    }
    static void chooseSwordsman() {
        System.out.println("You have chosen the Swordsman. You will be briefed and then you shall be set "
        +"on your quest!");  
    }
    static void chooseCrossbowman() {
        System.out.println("You have chosen the Crossbowman. You will be briefed and then you shall be set "
        +"on your quest!");
    }
}

プログラムを実行するたびに、クラスを選択するように求められます。選択肢を入力した後、プログラムは do ループの下の switch ステートメントに進みません。誰かがこれを修正するのを手伝ってくれますか?

-カルビン

4

2 に答える 2

2
while(repeat = true);

する必要があります: -

while(repeat == true);  // Or better: -  while(repeat);

そしてあなたのcatchで、次のように変更Input.nextLine()しますInput.next(): -

catch(InputMismatchException e) {
    repeat = true;
    System.err.println("Caught: "+e);
    Input.nextLine();  // Change to Input.next()
}

また、インスタンス変数は小文字のアルファベットまたはアンダースコアで始まる必要がありInputますinput

于 2012-10-06T12:26:27.910 に答える
2

これは課題です:

 while(repeat = true); // Note single '=', not '=='

Java言語仕様のtrueセクション15.26代入演算子から、その結果は常に になります。

実行時の代入式の結果は、代入が行われた後の変数の値です。

への変更:

while(repeat);
于 2012-10-06T12:26:44.690 に答える