当前位置: 首页 > 知识库问答 >
问题:

我们可以通过map调用一个函数,在Hashmap函数的值中传递函数名称吗

楚流觞
2023-03-14

我面临一个问题,我必须在网格中找到机器人的位置,它可以向前移动,可以改变面向北、南、东和西的方向,并具有给定的命令序列。那么,机器人的最终位置是什么呢。禁止使用任何类型的条件分支(例如if/else、switch/case)。

示例-
网格-(100*500)
机器人的初始位置-(5,3)
可能的命令-
北、东、西、南、向前移动
示例输入-{N、s、M.M、E、W、E、s、M、s、M、M}

我的Algo-
我在想我可以使用一个映射,其中我的键是命令,值是在该特定命令上执行的函数。但是我不知道如何通过HashMap值调用函数。此外,我们必须考虑网格不再有任何可能的移动的最终情况。

我按照建议尝试了这段代码,但不知道如何使用Enum给出动态命令

public class RobotMovesInGrid {
    Scanner input = new Scanner(System.in);
    String command=input.next();
    int commLength = command.length();

    static enum Command {
        N{@Override public void execute(String g, String r){ System.out.println("do the N move here"); }},
        E{@Override public void execute(String g, String r){ System.out.println("do the E move here"); }},
        S{@Override public void execute(String g, String r){ System.out.println("do the S move here"); }},
        W{@Override public void execute(String g, String r){ System.out.println("do the W move here"); }},
        M{@Override public void execute(String g, String r){ System.out.println("do the M move here"); }};
        public abstract void execute(String g, String r);

    }
    public void nextPosition() {
        Command c1;
        for(int i=0;i<commLength;i++) {
            if (command.charAt(i)=='N'||command.charAt(i)=='E'|| command.charAt(i)=='S'|| command.charAt(i)=='W'||command.charAt(i)=='M')

                c1= Command.M;// Here instead of M, I am trying to give dynamic commands but it is not taking it
            System.out.println("Current position is"+c1);
        }

    //return c1;
    }
}

有人可以建议我如何使用作为输入的命令调用枚举方法吗。

共有2个答案

端木澄邈
2023-03-14

对命令序列使用Map没有任何意义,因为map(尽管有子类)不会保留其元素的顺序。将命令映射到函数确实有意义,但有一种更简单的方法

这回答了“如何通过[a]值调用函数”的问题。

以下是可以执行方法的枚举模板:

static enum Command {
    N{@Override public void execute(Grid g, Robot r){ System.out.println("do the N move here"); }},
    E{@Override public void execute(Grid g, Robot r){ System.out.println("do the E move here"); }},
    S{@Override public void execute(Grid g, Robot r){ System.out.println("do the S move here"); }},
    W{@Override public void execute(Grid g, Robot r){ System.out.println("do the W move here"); }},
    M{@Override public void execute(Grid g, Robot r){ System.out.println("do the M move here"); }};
    public abstract void execute(Grid g, Robot r);
}

使用此枚举,您可以将序列存储在列表中,或传递枚举。

    Command c1 = Command.M; // hardcoded Move
    Command c2 = getNextMove(); // from a method: public Command getNextMove(){...}
    Command c3 = list.get(3); // from a collection: List<Command>

使用这些实例中的任何一个,您可以使用程序的适当实例调用函数:

    c1.execute(g, r);

对于g和r,五种方法中的每一种都应该有足够的信息来执行有效的机器人命令。

松烨烨
2023-03-14

首先,赋值参数似乎缺少一个“初始面向”属性或第一个命令永远不会是M的约束(因为如果您没有初始面向,如果第一个命令是M,您如何知道“前进”的方向?)

但是,忽略这个问题,您可以对此解决方案使用递归。给定网格大小、起始位置和移动列表...

public GridPosition findFinalPosition(Grid theGrid, GridPosition startPosition, List<MoveDirections> theMoves) {

    if (theMoves.length() == 0) {
       return startPosition;
    }
    GridPosition nextPosition = theGrid.findNextPosition(startPosition, theMoves.getAt(0));

    List<MoveDirections> remainingMoves = new ArrayList<MoveDirections>()
    for (int i = 1; i < theMoves.length(); i++) {
         remainingMoves.add(theMoves.getAt(i));
    }

    return findFinalPosition(theGrid, nextPosition, remainingMoves);

使用这样的递归方法,只需实现“findNextPosition”,而无需使用if/else或switch。。。(并控制离开网格的移动等)

由于这似乎是一个明显的类分配,我不打算向您展示如何在不使用if/else或switch的情况下实现“findNextPosition”。。。但是,提示提示,我的解决方案将实现一个类似于@MarkJeronimus的答案的枚举。

 类似资料:
  • 问题内容: 我需要将一个函数作为参数传递给另一个函数,然后从该函数内部调用传递的函数……这可能对我来说更容易用代码解释。我基本上想做这样的事情: 有没有办法做到这一点..我正在使用PHP 4.3.9 谢谢! 问题答案: 我想你在找。 PHP手册中的示例:

  • 问题内容: 如果您有两个功能,例如: 并且A呼叫B,您能知道谁在B内部呼叫B时,例如: 问题答案: 您可以使用检查模块获取所需的信息。它的堆栈方法返回帧记录列表。 对于 Python 2, 每个帧记录都是一个列表。每个记录中的第三个元素是呼叫者名称。您想要的是: def f(): … print inspect.stack()[1][3] … def g(): … f() … g() g 对于 P

  • rank ▲ ✰ vote url 59 383 111 805 url 通过函数名的字符串来调用这个函数 加入我们有个模块叫foo,而我有一个"bar"字符串.调用foo.bar()有什么最好的方法? 我需要返回函数值,为什么我不能用eval.我想应该能用eval来定义一个函数来返回调用的结果,但是我希望更优雅的方法. 假设foo有一个bar方法: import foo methodToCall

  • 所以,我试图用discord js和sqlite3做一件事,但在过去的几个小时里,我一直在讨论这个问题,我仍然不知道如何解决它。 因此,我试图获取仅在中可用的数据,但我不知道如何将其传递出去,以便返回预期值。我已经尝试过使用全局变量,把它放在更大的范围内,但我仍然不知道如何做到这一点。 对不起,如果我弄糊涂了,我不习惯一直问问题。 编辑:应该说我正在使用NPMSqlite3模块。https://w

  • 问题内容: 我对通过Go中的引用和值传递感到困惑。 我已经看到了在类型前面的*的解释。 在类型名称前面表示已声明的变量将存储该类型的另一个变量的地址(而不是该类型的值)。 这对我来说毫无意义。 在Java中,如果我将数据库实例传递给函数,我会这样做 但是在go示例中,我已经像这样通过了。 为什么我们需要在类型前面加上星号? 根据这份备忘单,我发现了。 func PrintPerson(p * Pe

  • 本文向大家介绍JavaScript函数的调用以及参数传递,包括了JavaScript函数的调用以及参数传递的使用技巧和注意事项,需要的朋友参考一下 JavaScript 函数调用 JavaScript 函数有 4 种调用方式。 每种方式的不同方式在于 this 的初始化。 this 关键字 一般而言,在Javascript中,this指向函数执行时的当前对象。 Note 注意 this 是保留关键