java 在交换机的情况下,有没有办法回到默认状态?

blpfk2vs  于 2022-12-02  发布在  Java
关注(0)|答案(2)|浏览(144)

我正在做一个tick tack脚趾cli游戏,需要用户输入并打印一个更新的董事会。其中一个功能,我希望它有是,它可以识别,已经有一个字符在插槽中,你想把你的一块。现在我有一个开关情况下,默认不改变任何东西。我可以参考它,或者如果我应该只是复制粘贴到每个情况下。

import java.util.Scanner;
public class TickTack {
    String[][] tickTackToe =
                        {{" ","|"," ","|"," "},
                        {"-","-","-","-","-"},
                        {" ","|"," ","|"," "},
                        {"-","-","-","-","-"},
                        {" ","|"," ","|"," "}};

    int xCoor = 0, yCoor = 0, counter = 1;

    //ignore this they do not matter yet
    int rOne = 0,rTwo = 0, rThree = 0;
    int cOne = 0, cTwo = 0, cThree = 0;
    int dOne = 0, dTwo = 0;
    String x = "";
    Scanner in = new Scanner(System.in);


    public void play() {
        while (!x.equals("win")){
            for (int fila = 0; fila < 5; fila++) {
                for (int columna = 0; columna < 5; columna++) {
                    System.out.print(tickTackToe[fila][columna]);
                }
                System.out.println();
            }
            boolean checker = false;
            while (!checker) {
                x = in.next();
                switch (x) {
                    //first row
                    case "1,1" -> {
                        xCoor = 0;
                        yCoor = 0;
                        checker = true;
                    }
                    case "1,2" -> {
                        xCoor = 0;
                        yCoor = 2;
                        checker = true;
                    }
                     case "1,3" -> {
                        xCoor = 0;
                        yCoor = 4;
                        checker = true;
                    }
                    //second row
                    case "2,1" -> {
                        xCoor = 2;
                        yCoor = 0;
                        checker = true;
                    }
                    case "2,2" -> {
                        xCoor = 2;
                        yCoor = 2;
                        checker = true;
                    }
                    case "2,3" -> {
                        xCoor = 2;
                        yCoor = 4;
                        checker = true;
                    }
                    //third row
                    case "3,1" -> {
                        xCoor = 4;
                        yCoor = 0;
                        checker = true;
                    }
                    case "3,2" -> {
                        xCoor = 4;
                        yCoor = 2;
                        checker = true;
                    }
                    case "3,3" -> {
                        xCoor = 4;
                        yCoor = 4;
                        checker = true;
                    }
                    default -> System.out.println("that is not a valid option");
                }
            }

            //check whose turn is it and alocate the piece
            counter ++;
            if (counter % 2 == 0){
                tickTackToe[yCoor][xCoor] = "x";
            }else{
                tickTackToe[yCoor][xCoor] = "o";
            }
        }
    }
}

我看了一堆switch语句教程(w3学校,geeks for geeks,javapoint),但我什么都没找到

pkwftd7m

pkwftd7m1#

您无法恢复为默认值,也不必恢复。
如果在案例中检测到该字段已被使用,只需打印消息field already occupied,然后使用break结束案例。这与用户发出不可预见的命令不同,在默认案例中将打印that is not a valid option。因此没有剪切和粘贴。

2izufjch

2izufjch2#

这是个坏主意:

switch (x) {
//first row
case "1,1" -> {
  xCoor = 0;
  yCoor = 0;
    checker = true;
}
case "1,2" -> {
  xCoor = 0;
  yCoor = 2;
  checker = true;
}...

这里根本不应该使用switch语句。可以尝试这样做:

boolean checker = false;
while (!checker) {
  x = in.next().trim();
  String [] parts = x.split(",");
  try {
    xCoor = (Integer.parseInt(parts[0]) - 1) * 2;
    yCoor = (Integer.parseInt(parts[1]) - 1) * 2;
    checked = (xCoor >= 0 && xCoor <= 4
            && yCoor >= 0 && yCoor <= 4
            && tickTackToe[yCoor][xCoor].equals(" "));
  } catch(IndexOutOfBoundsException | NumberFormatException ex) {
    // fall through with checked still false
    assert checked == false;
  }

  if (!checker) {
    System.out.println("that is not a valid option");
  }
}

相关问题