0

我有我的迷宫,可以去东北西南,但我如何移动 ne 或 sw。如果可能的话,你们可以给我使用我的代码的例子。感谢它的 4x4 阵列。

用户界面

    //Begin user dialog
    System.out.println("Welcome");
    input ="";
    while(!input.equals("quit"))
    {
       System.out.println(map.rooms[row][col].name);
       System.out.print(">");
       input = scan.nextLine().toLowerCase();

        switch (input) {
            case "n":
                if(map.rooms[row][col].isValidExit("n"))
                    row--;
                else
                    System.out.println("You cant go that way");
                break;
            case "s":
                 if(map.rooms[row][col].isValidExit("s"))
                    row++;
                else
                    System.out.println("You cant go that way");
                 break;
            case "w":
                if(map.rooms[row][col].isValidExit("w"))
                    col--;
                else
                    System.out.println("You cant go that way");
                break;
            case "e":
                if(map.rooms[row][col].isValidExit("e"))
                    col++;
                else
                    System.out.println("You cant go that way");
                break;
4

2 回答 2

1

你需要对你的输入进行字符分析

while(!input.equals("quit"))
{
   System.out.println(map.rooms[row][col].name);
   System.out.print(">");
   input = scan.nextLine().toLowerCase();
   char[] inputArray = input.toCharArray();

   for(char c : inputArray){

    switch (input) {
        case "n":
            if(map.rooms[row][col].isValidExit("n"))
                row--;
            else
                System.out.println("You cant go that way");
            break;
        case "s":
             if(map.rooms[row][col].isValidExit("s"))
                row++;
            else
                System.out.println("You cant go that way");
             break;
        case "w":
            if(map.rooms[row][col].isValidExit("w"))
                col--;
            else
                System.out.println("You cant go that way");
            break;
        case "e":
            if(map.rooms[row][col].isValidExit("e"))
                col++;
            else
                System.out.println("You cant go that way");
            break;
于 2013-11-15T10:01:54.147 回答
0

例如东北:

    case "ne":
        if(map.rooms[row][col].isValidExit("ne")) {
            col++;
            row--;
        }
        else {
            System.out.println("You cant go that way");
        }
        break;
于 2013-11-15T10:04:18.923 回答