gpt4 book ai didi

java - 找不到无限循环的原因

转载 作者:行者123 更新时间:2023-12-02 03:04:37 25 4
gpt4 key购买 nike

我的代码导致了无限循环,我只是找不到在哪里,这让我发疯。对此有什么帮助吗?它必须采用以下三种方法之一。它涉及一个链表。任何帮助都是极好的。谢谢

 public boolean checkNotSamePlacements() {

Link current = head;

while (current != null) {

Link current2 = head;

while (current2 != null) {

if (current != current2) {

if (current.piece.col == current2.piece.col && current.piece.row == current2.piece.row) {

return true;
}
}
current2 = current2.next;
}
current = current.next;
}

return false;
}


public void checkAttacking () {
boolean foundPieces = false;
Link current = head;

while (current != null) {

Link current2 = head;
while (current2 != null) {

if (current != current2) {

if ((current.piece.isAttacking(current2.piece)) && foundPieces == false) {

System.out.print(current.piece.pieceType + " " + current.piece.col +
" " + current.piece.row + " " + current2.piece.pieceType +
" " + current2.piece.col + " " + current2.piece.row);
foundPieces = true;
}
}
current2 = current2.next;
}
current = current.next;
}
if (foundPieces == false) {
System.out.print("-");
}
}

public void checkSpotFound (int col, int row) {

boolean foundPiece = false;
Link current = head;

while (current != null) {

if (current.piece.col == col && current.piece.row == row) {

System.out.print(current.piece.pieceType);

foundPiece = true;
}
}

if (foundPiece == false) {
System.out.print("-");
}
}

}

最佳答案

代码中的当前永远不会更新 -

while (current != null) {

if (current.piece.col == col && current.piece.row == row) {

System.out.print(current.piece.pieceType);

foundPiece = true;
}

current = current.next; // you might want to add this to your code
}

关于java - 找不到无限循环的原因,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41907172/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com