gpt4 book ai didi

java - 我破坏了 Java while() 循环 - 在错误的条件下触发并在正确的条件下卡住

转载 作者:行者123 更新时间:2023-12-01 23:03:34 25 4
gpt4 key购买 nike

我正在尝试为算法类(class)编写一个跳跃列表,但我的传播方法存在问题(在 A_prop 和 B_prop 之间找到插入点后调用)。代码如下:

/* The randomized propagation upwards */
private void propagate(SkipNode<T> A_prop, SkipNode<T> B_prop,
SkipNode<T> front_prop, SkipNode<T> end_prop,
SkipNode<T> insert)
{
SkipNode<T> clone, rowBelow;
Random random = new Random();
float f = random.nextFloat();
int flip = Math.round(f);

A_prop.linkRight(insert);
B_prop.linkLeft(insert);
rowBelow = insert;

System.out.println("Flip = " + flip);
while(flip != 0);
{
System.out.println("HEADS");
clone = insert.clone();

// Insert node at this level
A_prop.linkRight(clone);
B_prop.linkLeft(clone);
clone.linkDown(rowBelow);

System.out.println("After Link");
System.out.println(A_prop.right);
System.out.println(insert);
System.out.println(insert.right);
System.out.println(B_prop);

if(A_prop.up != null)
{ // Move up a row
System.out.println("A up");
A_prop = A_prop.up;
B_prop = A_prop.right;
front_prop = front_prop.up;
end_prop = end_prop.up;
rowBelow = clone;
}
else if(front_prop.up != null)
{
System.out.println("Front up");
while(A_prop.up == null)
{ // Find the closest node to the left that
// has a copy in the above level
A_prop = A_prop.left;
}

A_prop = A_prop.up;
B_prop = A_prop.right;
front_prop = front_prop.up;
end_prop = end_prop.up;
rowBelow = clone;
}
else // Create new top level
{
System.out.println("New row");
// Make new terminal nodes
A_prop = new SkipNode<T>("HEAD");
B_prop = new SkipNode<T>("TAIL");
// Link them to the current ones
front_prop.linkUp(A_prop);
end_prop.linkUp(B_prop);
// Update current
front_prop = front_prop.up;
end_prop = end_prop.up;
// Link 'em
front_prop.linkRight(end_prop);
// To be sure
A_prop = front_prop;
B_prop = end_prop;

rowBelow = clone;
}

f = random.nextFloat();
flip = Math.round(f);
}

// Don't forget to update the global head and tail for the list
head = front_prop;
tail = end_prop;
}

我仍然有一些调试 println() 的地方;我在控制台上得到的输出是以下两种情况之一:

情况1:

翻转 = 1

情况2:

翻转=0

在情况 1 中,程序仍在运行(我需要点击控制台上的小红框才能中断),但不再执行任何代码。我已将 println() 放在 while 循环之后,没有任何打印,并且我已注释掉循环内的所有内容 - 没有效果。我的计算机运行噪音很大而且很热,直到我重新启动 Eclipse。

在情况 2 中,程序继续运行,直到遇到情况 1...

所有四种链接方法都经过测试并且有效。我尝试过重新启动计算机,甚至重新安装了 Java(版本 1.7.0_45)。我不知道是什么原因造成的,非常感谢您的帮助。

谢谢!

最佳答案

请检查这行代码...不应该有任何分号来终止 while 循环!!!

while(flip != 0);

关于java - 我破坏了 Java while() 循环 - 在错误的条件下触发并在正确的条件下卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23165488/

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