简体   繁体   English

如何更改我的代码以允许我的回溯命令多次工作?

[英]How can I alter my code to allow my retrace command to work more than once?

Before I get into my issue here is a quick little summary of my program in relation to my issue so you understand.在我进入我的问题之前,这里是我的程序与我的问题相关的快速小总结,以便您理解。 My program relates to a robot, below represents some commands that my program executes to the robots.我的程序与机器人有关,下面代表我的程序对机器人执行的一些命令。 The commands inputted by the user are stored in the commandList.size array.用户输入的命令存储在 commandList.size 数组中。

  • L command means the robot turns left. L 命令表示机器人左转。
  • F command means the robot moves forwards. F 指令表示机器人向前移动。
  • T command means the robot retraces the commands backwards from which the user inputted into the program. T命令是指机器人向后回溯用户输入程序的命令。 For example, if the user already entered the commands "L COMMAND, R COMMAND" then the user enters "T 2" this will need to make the robot turn right then turn left so the array of commands is being read backwards.例如,如果用户已经输入命令“L COMMAND, R COMMAND”,则用户输入“T 2”,这将需要使机器人右转然后左转,以便向后读取命令数组。 "T 2" means retrace the previous last two commands. “T 2”表示回溯前两个命令。

ISSUE问题

If the user makes my robot turn left then the user enters “ T 1” to retrace the previous inputted commands, then after the user makes the robot go forward then types in “ T 2” to retrace the previous two commands the program stops working.如果用户让我的机器人向左转,则用户输入“T 1”以回溯先前输入的命令,然后在用户使机器人前进后键入“T 2”以回溯前两个命令程序停止工作。 The program should make the robot go forward then turn left as they were two last commands entered.程序应该让机器人前进然后左转,因为它们是最后输入的两个命令。 The “T 1” command needs to be ignored as that's not counted. “T 1”命令需要被忽略,因为它没有被计算在内。 How can I get my program to successfully retrace the commands more than once?如何让我的程序多次成功回溯命令?

Another example另一个例子

L COMMAND (Turn left)
"T 1" (Retrace the left command - this work)
B COMMAND (Move backwards)
"T 2" (THIS IS WHERE THE PROGRAM STOPS WORKING - the program needs to skip "T 1" and retrace the F command / B command - Here is the issue) you understand? 

This issue above is to do with the T command I'm sure within my program.上面的这个问题与我确定在我的程序中的 T 命令有关。

CONSOLE EXAMPLE TO HIGHLIGHT THE ISSUE - This doesn't work.突出问题的控制台示例- 这不起作用。 The robot should go backwards and move to the right.机器人应该向后移动并向右移动。 The T 1 command should be ignored. T 1 命令应该被忽略。

>Move robot right 
>T 1 to retrace last step which works
>Backwards command
>T 2 retrace last two commands. 

https://codepen.io/code https://codepen.io/code

I think I understand what the issue is.我明白问题是什么。 Your commandList collection should always hold every command supplied by User unless the supplied command is invalid.您的commandList集合应始终保存用户提供的每个命令,除非提供的命令无效。 This way you can always retrace the actions already done.通过这种方式,您可以随时回溯已完成的操作。 It's just a matter of reading through that list in reverse order and ignoring the Retrace (or back-step) commands.这只是以相反的顺序阅读该列表并忽略回溯(或后退)命令的问题。 See if the following code helps you out a little.看看下面的代码是否对你有所帮助。 It is well commented:评论很好:

Scanner input = new Scanner(System.in);
List<String> commandList = new ArrayList<>();
String command = "";

while (!command.equals("Q")) {
    System.out.println("Enter Robot Command: ");
    command = input.nextLine().trim().toUpperCase();

    // Is 'Q'uit desired?
    if (command.equals("Q")) {
        break; // Break out of WHILE loop
    }
    // Validate Command Input!
    if (!command.matches("(?i)[LRBF]{1}\\s+\\d{1}\\s+\\d+|(?i)[T]\\s{0,}\\d+")) {
        System.out.println("INVALID INPUT!. Try Again...");
        continue; // Prompt again...
    }

    // Add command to list
    commandList.add(command);

    // Is a Back-Step command issued?
    if (command.startsWith("T")) {
        // Yes...get the step number from the command
        int step = Integer.parseInt(command.replaceAll("\\s+", "").split("")[1]);
        // Is the Back-Step Valid?
        if (step > (commandList.size() - 1)) {
            // NO it's not
            System.out.println("INVALID BACKSTEP INPUT!. Robot hasn't moved "
                             + "that many steps! Try Again...");
            // Remove failed Back-Step command from list.
            commandList.remove(commandList.size() - 1);
            continue;  // Prompt again...
        }
        List<String> backStep = new ArrayList<>(); // Used to hold all back-steps.

        // Last index value of commandList (NOT includinmg the Back-step command).
        int cmdLastIndex = commandList.size() - 2; 

        // Acquire the Back-Steps from the commandList in 
        // reverse order from end to start.
        int stepCount = 0; // Keep track of steps gong back
        for (int i = cmdLastIndex; i >= 0; i--) {
            // If you DON'T want to ignore 'T' commands in 
            // back-step then comment the following IF block:
            if (commandList.get(i).startsWith("T")) {
                continue;
            }
            backStep.add(commandList.get(i)); // Add to backStep List
            moveBot(commandList.get(i));      // Move the Bot this particular step location.
            stepCount++;                      // Back-Step oount increment (by 1).
            if (stepCount == step) { break; } // If step count = step, exit FOR loop.
        }

        // For console display purpose...
        System.out.println(backStep);  // Display to console. 
        // Do whatever you want with the backStep List.

        continue;   // Prompt again for Bot movement input...
    }

    // Not a Back-Step so....
    moveBot(command);   // Move the Bot this particular step location.
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何阻止我的代码多次打印“升序”? - How to stop my code from printing "Ascending" more than once? 我怎样才能让我的数组多次替换一个值? - How can I get my array to replace a value more than once? 在我的testNG集成测试中,我可以多次使用@factory吗(使用Jenkins和Maven进行构建)? - In my testNG integration tests can I use @factory more than once (using Jenkins and Maven for my builds)? 我正在用Java制作命令行音乐播放器,而“跳过”按钮有时会跳过多次 - I'm making a command line music player in java, and my 'skip' button is sometimes skipping more than once 为什么我的反向 LinkedList 方法不能多次工作? - Why wont my reverse LinkedList method not work more than once? 为什么我的代码不会随机生成多次? - Why isn't my code randomly generating more than once? 我该如何更改我的数组列表? - How can i alter my arraylist? 我怎样才能让我的 Jump Animation 同时工作? - How Can I Make My Jump Animation Work All At Once? 如果我使用Freemarker的字符串长度超过一个字符,该如何创建执行命令的条件? - How can I create a condition where a command is executed if my string length is more than one character using Freemarker? 这段代码的问题在于输出。 生产者生产不止一次。 为什么以及如何解决? - The problem of this code is in the output. Producer produce more than once. Why and how can I solve it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM