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);

在你的捕获中,更改Input.nextLine()Input.next(): -

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

并且您的实例变量应该以小写字母或下划线开头。所以更改Inputinput.

于 2012-10-06T12:26:27.910 回答
2

这是一个作业:

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

其结果将始终为true,来自Java 语言规范的第15.26 节赋值运算符:

在运行时,赋值表达式的结果是赋值发生后变量的值。

改成:

while(repeat);
于 2012-10-06T12:26:44.690 回答