【问题标题】:Java Loop is not doing what It should be doingJava 循环没有做它应该做的事情
【发布时间】:2012-03-27 16:37:46
【问题描述】:

递归的 conflictCheck() 方法有问题。现在看来还不错。我有用于测试的打印线,看起来不错。现在,当发生冲突时(返回 true),我的 playChess() 方法应该通过调整节点的第二个变量来解决冲突,依此类推。

我的结果是字面意思:

1,1

2,3

3,1

4,3

5,1 ...

即使输出显示“哦,我们有冲突”,它也不会在达到 x,3 时对其采取行动

    public static boolean conflictCheck(QueenNode a, QueenNode b) {
    //checks for conflicts between head and all other nodes in the stack
    if (b == null) {
        System.out.println("Attempting Conflict Check: Nothing to Compare to");
        return false;
    }

    if (a.getRow()!=b.getRow() && a.getColumn()!=b.getColumn() && !diagonal(a,b)){
        System.out.println("Comparing " + a.getRow() + " ," + a.getColumn() +
                                    " And " + b.getRow() + " , " + b.getColumn());
        conflictCheck(a,b.getNext());
    }
    else {
        System.out.println("There is a conflict with " +a.getRow() + "," + a.getColumn()
                            + " And " + b.getRow() + "," + b.getColumn());
        return true;
        }
    return false;
}


    public static void playChess() {
    System.out.println("Playing chess");
    //Either there is a conflict between head and another node, the stack isn't full, or we have solution
    if (conflictCheck(head, head.getNext())) {
        if (head.getColumn() == 8) {
            queens.pop();
        }
        else if (!queens.isEmpty()) {
            System.out.println("Adjusting head");
            head.setColumn(head.getColumn()+1);
            System.out.println("Head is now " + head.getRow() + ", " + head.getColumn());
            playChess();

        }
    }

    else if (queens.size() < 8) {
        System.out.println("Stack isn't full yet");
        queens.push(queens.size()+1,1);
        queens.viewPieces();
        playChess();
        }
    else {
        success= true;
        System.out.println("Success");
        queens.viewPieces();
        return;
    }
}

整个输出:

The stack
1, 1
End of stack
Playing chess
Attempting Conflict Check: Nothing to Compare to
Stack isn't full yet
The stack
2, 1
1, 1
End of stack
Playing chess
There is a conflict with 2,1 And 1,1
Adjusting head
Head is now 2, 2
Playing chess
problem
There is a conflict with 2,2 And 1,1
Adjusting head
Head is now 2, 3
Playing chess
Comparing 2 ,3 And 1 , 1
Attempting Conflict Check: Nothing to Compare to
Stack isn't full yet
The stack
3, 1
2, 3
1, 1
End of stack
Playing chess
Comparing 3 ,1 And 2 , 3
There is a conflict with 3,1 And 1,1
Stack isn't full yet
The stack
4, 1
3, 1
2, 3
1, 1
End of stack
Playing chess
There is a conflict with 4,1 And 3,1
Adjusting head
Head is now 4, 2
Playing chess
problem
There is a conflict with 4,2 And 3,1
Adjusting head
Head is now 4, 3
Playing chess
Comparing 4 ,3 And 3 , 1
There is a conflict with 4,3 And 2,3
Stack isn't full yet
The stack
5, 1
4, 3
3, 1
2, 3
1, 1
End of stack
Playing chess
Comparing 5 ,1 And 4 , 3
There is a conflict with 5,1 And 3,1
Stack isn't full yet
The stack
6, 1
5, 1
4, 3
3, 1
2, 3
1, 1
End of stack
Playing chess
There is a conflict with 6,1 And 5,1
Adjusting head
Head is now 6, 2
Playing chess
problem
There is a conflict with 6,2 And 5,1
Adjusting head
Head is now 6, 3
Playing chess
Comparing 6 ,3 And 5 , 1
There is a conflict with 6,3 And 4,3
Stack isn't full yet
The stack
7, 1
6, 3
5, 1
4, 3
3, 1
2, 3
1, 1
End of stack
Playing chess
Comparing 7 ,1 And 6 , 3
There is a conflict with 7,1 And 5,1
Stack isn't full yet
The stack
8, 1
7, 1
6, 3
5, 1
4, 3
3, 1
2, 3
1, 1
End of stack
Playing chess
There is a conflict with 8,1 And 7,1
Adjusting head
Head is now 8, 2
Playing chess
problem
There is a conflict with 8,2 And 7,1
Adjusting head
Head is now 8, 3
Playing chess
Comparing 8 ,3 And 7 , 1
There is a conflict with 8,3 And 6,3
Success
The stack
8, 3
7, 1
6, 3
5, 1
4, 3
3, 1
2, 3
1, 1
End of stack

【问题讨论】:

  • 请显示您的完整输出。
  • 现在添加,我将编辑我原来的问题

标签: java loops methods


【解决方案1】:

由于这是作业,所以有一个小提示:

您未对检测到的冲突采取行动的原因很可能是因为您忽略了从 boolean conflictCheck(QueenNode a, QueenNode b) 递归调用的 conflictCheck(a,b.getNext()); 的返回值。

第 72 页上的 this excellent work by E.W.Dijkstra 深入探讨了八皇后的问题。它是用 ALGOL-60 编码的(甚至在我之前就已经过时了),但这些想法与语言无关。

【讨论】:

  • 我不明白为什么它会忽略价值
  • @JackieAldama 返回值被忽略,因为您在 conflictCheck 上调用 getNext(),但是当布尔值返回给您时,您的代码既不返回它也不将其分配给局部变量以供进一步分析。
  • 虽然分解该方法,但每次比较发生时,它应该打印出“比较 x 和 y”,但它只在第一次比较时打印。我知道它正在比较堆栈中的其他人,因为我可以让它打印“与 x 和 z 冲突”,所以它正在进行比较。这超出了布尔返回的范围,不是吗??
  • @JackieAldama 哦,除了忽略布尔值的返回之外,肯定还有其他东西。不过,在进一步调试程序之前,您确实需要将其整理好。我建议将其分成两部分:一个比较两个单个节点,忽略getNext,另一个比较列表中的单个节点而不递归,假设单个节点到节点的比较器工作正常。这将极大地简化您的逻辑和调试输出。
猜你喜欢
  • 1970-01-01
  • 2011-09-28
  • 2020-07-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多