Java 异常处理 Do 循环不起作用?

标签 java exception loops

这是代码:

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

每次运行它时,程序都会提示我选择我的类(class)。输入我的选择后,程序不会继续执行 do 循环下面的 switch 语句。有人可以帮我解决这个问题吗?

-卡尔文

最佳答案

这是一项作业:

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

其结果将始终为 true,来自 15.26 Assignment Operators 部分Java 语言规范:

At run time, the result of the assignment expression is the value of the variable after the assignment has occurred.

更改为:

while(repeat);

关于Java 异常处理 Do 循环不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12759775/

相关文章:

java - 如何在 JTextArea 中使用荧光笔

java - Java中的基本I/O问题

arrays - 使用循环查找特定数组

javascript - 为什么这个 javascript 循环会无限返回?

java - 单一职责原则与贫/富域模型有何关系?

java - 如果对象在Elastic search中具有对象列表,那么我们可以在一个文档中对数据进行排序吗?我还应该对嵌套文档进行排序

java - 如何从外部 Maven 依赖项中添加排除某些包

java 。为什么这个for循环不能正常工作

java - 审计API请求/接口(interface)方法的机制/技术

exception - 任务 : ':app:preDexDebug' 执行失败