Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Java中向switch语句添加同义词?_Java_Synonym - Fatal编程技术网

如何在Java中向switch语句添加同义词?

如何在Java中向switch语句添加同义词?,java,synonym,Java,Synonym,我有下面的函数,我想为每个案例添加同义词,例如案例“north”也可以是“up”,案例“east”可能是正确的等等。由于所有案例都属于同一个try/catch语句,我不确定如何为每个特定案例添加同义词。谢谢你的帮助!谢谢 public void changeRoom(boolean isValidInput, String[] input, int attemptCount) { while (isValidInput) { switch (input[1

我有下面的函数,我想为每个案例添加同义词,例如案例“north”也可以是“up”,案例“east”可能是正确的等等。由于所有案例都属于同一个try/catch语句,我不确定如何为每个特定案例添加同义词。谢谢你的帮助!谢谢

public void changeRoom(boolean isValidInput, String[] input, int attemptCount) {
        while (isValidInput) {
            switch (input[1]) {
                case "north":
                case "east":
                case "south":
                case "west":
                    try {
                        if (world.getCurrentRoom().roomExits.containsKey(input[1])) {
                            player.setMostRecentExit(input[1]);
                            world.setCurrentRoom(world.getCurrentRoom().roomExits.get(input[1]));
                            isValidInput = false;
                            if (isSound) {
                                walkEffect.playSoundEffect();
                            }
                            Thread.sleep(1800);
                            narrateRooms(world.getCurrentRoom().getDescription());
                            break;
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                default:
                    System.out.println("You hit a wall. Try again: ");
                    System.out.print(">>>");
                    attemptCount++;
                    if (attemptCount >= 2) {
                        System.out.println();
                        openMap();
                        System.out.println("Where would you like to go? ");
                        System.out.print(">>>");
                    }
                    input = scanner.nextLine().strip().toLowerCase().split(" ");
                    break;
            }
        }

就这样列出这些案例:

switch (input[1]) {
            case "north": case "up":
            case "east": case "right":
            case "south": case "down":
            case "west": case "left":

我能够通过创建下面的函数来规范化输入。谢谢大家

public String normalizeText(String input) {
    List<String> northOptions = Arrays.asList("north", "up");
    List<String> southOptions = Arrays.asList("south", "down");
    List<String> eastOptions = Arrays.asList("east", "right");
    List<String> westOptions = Arrays.asList("west", "left");
    if (northOptions.contains(input.toLowerCase())) {
        return "north";
    }
    if (southOptions.contains(input.toLowerCase())) {
        return "south";
    }
    if (eastOptions.contains(input.toLowerCase())) {
        return "east";
    }
    if (westOptions.contains(input.toLowerCase())) {
        return "west";
    }
    return "";
} 
公共字符串normalizeText(字符串输入){
List northOptions=Arrays.asList(“north”、“up”);
List southOptions=Arrays.asList(“南”、“下”);
List eastpoptions=Arrays.asList(“东”、“右”);
List westOptions=Arrays.asList(“西”、“左”);
if(northOptions.contains(input.toLowerCase())){
返回“北”;
}
if(southOptions.contains(input.toLowerCase())){
返回“南方”;
}
if(eastOptions.contains(input.toLowerCase())){
返回“东方”;
}
if(westOptions.contains(input.toLowerCase())){
回归“西方”;
}
返回“”;
} 

相同的机制,或者更好的机制,通过某种规范化程序运行输入。注意,“向上”仅适用于2d世界中的北方。您可以定义同义词的地图,例如
map.of(“向上”、“向北”、“向右”、“向东”/*etc*/)
,然后打开例如
map.getOrDefault(输入[1]。toLowerCase(),输入[1]。toLowerCase())
而不是谢谢您的提示。我试过了,但没有成功。我正试图理解如何规范化它,但我自己没有足够的经验知道如何做到这一点。为了完整起见,我可能会添加这一点,因为第一个字符串和它的别名之间没有break语句,它必须位于每个case的前面,以便区分一个case和另一个case(例如:
case)“north”:case“up”:[…]break;
)。谢谢Maurizio!我在try/catch块中有一个break语句,我尝试在每个case之间添加分号,如下所示,但仍然不起作用。我需要try/catch块外的break语句吗?case“north”:case“up”:case“east”:case“right”:case“south”:case“down”:case“west”:case“left”:我不在电脑旁,无法确认,所以我可能说错了什么,但是。。。看起来try块确实有自己的作用域,特别是当抛出异常且中断未执行时,这会让我说是的,我会尝试在该作用域之外使用中断,但仍在别名案例中。不幸的是,我已经很久没有阅读Java标准了,所以我对try构造中的中断行为没有清晰的记忆。在任何情况下,你说的“不起作用”是什么意思?默认块被执行了还是什么?@Mauriziocarcasona你完全正确,谢谢你纠正我:)我看到你走了一条间接的路——我也会这么做:)干得好!