Java扫描命令系统

Java Scanner command System

我必须创建一个 java class,我可以在其中从标准控制台读取一些命令。这就像模拟运动进入网格。我很难准确地创建我想要的东西。假设我有这个命令:

"X" 和 "Y" 是矩阵 6x6 的坐标。 "DIRECTION" 可以是 "UP"、"DOWN"、"LEFT"、"RIGHT"。如果我写 "STEP" 我会做一步。

程序应该放弃 STEP 命令,直到执行了有效的 START 命令。之后,我可以使用 STEP 或其他有效的 START 命令,它将使用新坐标放置“1”,删除第一个

示例:

a)START 1,4,UP ---> OK! ANOTHER START OR STEP COMMAND
  STEP ---> OK MOVE!

b)START 3,5,UP ---> OK! ANOTHER START OR STEP COMMAND
  START 5,2,LEFT ---> DONE! OK NEW POSITION!
  STEP ---> OK MOVE!

c)STEP ---> NO! I NEED START
  OWINEVEIVNEW ---> NO! I NEED START
  START 3,2,RIGHT ---> OK! ANOTHER START OR STEP COMMAND

如果我有 START 命令,我还必须捕获坐标 (X,Y) 和方向。

我的想法是:

public static void main(String[] args) {
    Movement grid = new Movement(6); --> call constructor. 6 is dimension square grid
    System.out.println("**********INSERT COMMANDS**********");
    while(true){ --> loop to continue to insert commands
        if (grid.startCommand()) {
           System.out.println("OK START RECEIVED! I CAN USE STEP");
           grid.stepForward();
        } else {
           System.out.println("I can't go ahead without valid START command. Try again!");
        };
    }   
}

public boolean stepForward() {
    if (start.equals("STEP") {
        System.out.println("OK LET'S DO A STEP!!");
    }
    return true;
}

public boolean startCommand() {
    Scanner sc = new Scanner(System.in);
    String start = sc.next();
    sc.useDelimiter("\s+");
    String[] coordinates = sc.next().split(",");
    X = Integer.parseInt(coordinates[0]);
    Y = Integer.parseInt(coordinates[1]);
    direction = coordinates[2];
    while(start.equals("START")) {
        if ((0<=X && X<dim) && (0<=Y && Y<dim)){
            if (direction.equals("UP")||direction.equals("DOWN")||direction.equals("LEFT")||direction.equals("RIGHT")){
                cleanGrid(); --> this is just a method that put everything at 0
                matrix[X][Y] = 1;
                return true;
            } else {
                System.out.println("Your direction doesn't exist. Use \"UP\",\"DOWN\",\"LEFT\",\"RIGHT\".Try again!");
                return false;
            }
        } else {
            System.out.println("Check range of X and Y. Try again!");
            return false;
        }
    } 
    System.out.println("Insert command like ex. \"START 1,2,UP\". Try again!");
    return false;
}

是的,我在 IF、WHILE 等之间迷路了...我尝试了不同的解决方案,但或者我失去了插入另一个 START 的可能性,或者我无法识别 STEP 命令或其他类型的问题。 有人可以帮我解决这个问题吗?提前致谢。

以不同的方式思考您的问题...

  • 您提示用户输入
  • 用户输入了一些内容
  • 您需要确定输入是什么并对它执行一些操作
  • Return 到提示用户

这表明您需要先识别用户输入的命令,然后才能执行命令背后的逻辑,例如...

Scanner scanner = new Scanner(System.in);
boolean exit = false;
do {

    System.out.print("CMD> ");
    String input = scanner.nextLine();
    if ("exit".equalsIgnoreCase(input)) {
        exit = true;
    } else if (input.toLowerCase().startsWith("start")) {
        doStart(input);
    } else if (input.toLowerCase().startsWith("step")) {
        doStep(input);
    }

} while (!exit);

一旦知道了用户输入的命令,就知道要执行哪个方法来执行命令。

然后,根据命令,可能需要解析参数并进行处理...

protected void doStart(String input) {
    Scanner scanner = new Scanner(input);
    scanner.next(); // Command
    String parameters[] = scanner.next().split(",");
    int x = Integer.parseInt(parameters[0]);
    int y = Integer.parseInt(parameters[1]);
    String dir = parameters[2];

    System.out.println(" > start @ " + x + "x" + y + " in " + dir + " direction");
}

Like your code say I can write "start 2,3,up" and go ahead with a "step" but I can write also "step" like first command

你需要一些可以维持当前状态的东西,例如...

public class BotPos {

    private int x;
    private int y;
    private String direction;

    public BotPos(int x, int y, String direction) {
        this.x = x;
        this.y = y;
        this.direction = direction;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public String getDirection() {
        return direction;
    }

    public void setDirection(String direction) {
        this.direction = direction;
    }

    @Override
    public String toString() {
        return " > Bot is @ " + x + "x" + y + " in " + direction + " direction";
    }
}

假设 botPos 是一个 class 实例字段,然后开始将设置此状态...

protected void doStart(String input) {
    Scanner scanner = new Scanner(input);
    scanner.next(); // Command
    String parameters[] = scanner.next().split(",");
    int x = Integer.parseInt(parameters[0]);
    int y = Integer.parseInt(parameters[1]);
    String dir = parameters[2];

    botPos = new BotPos(x, y, dir);
    System.out.println(botPos);
}

如果可以的话doStep会更新它...

protected void doStep(String input) {
    if (botPos != null) {

        switch (botPos.getDirection().toLowerCase()) {
            case "up":
                botPos.setY(botPos.getY() - 1);
                break;
            case "down":
                botPos.setY(botPos.getY() + 1);
                break;
            case "left":
                botPos.setX(botPos.getX() - 1);
                break;
            case "right":
                botPos.setX(botPos.getX() + 1);
                break;
        }
        System.out.println(botPos);

    } else {

        System.out.println(" > Invalid state, you have no start position");

    }
}

现在,您也可以将 botPos 传递给方法,但思路是一样的。

如果需要,您只需将 botPos 设置为 null 即可使其无效