如何在Java中消除Nim程序中的递归?

如何在Java中消除Nim程序中的递归?,java,Java,我正在制作Nim的游戏,我知道我在某个地方经历过递归,但我不确定如何修复它。有人能帮我一下吗? 下面是您应该需要的代码,但如果需要,我可以获得更多 提前谢谢 public static void changeDisplay() { //checks if the group picked equals one of the existing groups. if(groupPick.contentEquals("A") || groupPick.contentEquals("a"

我正在制作Nim的游戏,我知道我在某个地方经历过递归,但我不确定如何修复它。有人能帮我一下吗? 下面是您应该需要的代码,但如果需要,我可以获得更多

提前谢谢

public static void changeDisplay() {
    //checks if the group picked equals one of the existing groups.
    if(groupPick.contentEquals("A") || groupPick.contentEquals("a")) {
        //checks if the group is equal to 0, avoids cheating.
        if(a == 0) {
            System.out.println("Nice try " + playerName + ", you can't take something from nothing.");
            gamePlaying();
        } else {
            //checks if the number picked is greater than the amount of elements in the group selected.
            while(numPick > a) {
                System.out.print("Error, you cannot take more than what is there. Please pick again: ");
                numPick = new Scanner(System.in).nextInt();
            }

        }
跳过了一些if语句

} else {
        //runs this code if the group the player selected isn't a pre-existing group.
        System.out.println("Oops! "+ groupPick + " doesn't seem to be a group, please select a group (A, B, or C).");
        System.out.print(playerName + ", choose a pile: ");
        groupPick = new Scanner(System.in).nextLine();
        changeDisplay();
    }
        playerPicker++;
        display();
    } 

要停止递归,您需要

需要再次调用changeDisplay以返回bool,指示它是否仍然脏


B用whiletrue{…}包装changeDisplay函数的内容以重复该过程,完成后使用return/break停止。

有什么问题?不按预期工作或存在异常?为什么要放弃递归?你的实际问题是什么?跳过了一些if语句。。。我有一种感觉,我们需要这些if语句,因为您可以在其中无条件地再次调用changesplay。请至少在else之前发布if语句。@Carcigenicate我对此没有异议,我的程序运行顺利,但我的编程导师告诉我避免递归,因为它可能会在将来导致问题。