gpt4 book ai didi

Java:递归从未达到正确的条件

转载 作者:行者123 更新时间:2023-11-30 04:13:17 25 4
gpt4 key购买 nike

这是我的自递归方法的代码。问题是第三个递归,比较<3.0, 4.0, 4.0><2.0, 4.0, 1.0> ,它应该命中情况 3,但从显示的日志来看,它从未命中。

private static List<Position> overlaps (List<Position> sortedlist)  
{

List<Position> tony = new ArrayList<Position>();

if(count<=sortedlist.size()-1)
{
//handle a
Position a = sortedlist.get(count);

System.out.println("**new recursive start!");
System.out.println("sortedlist size is:"+sortedlist.size());
System.out.println("overlapnum is:"+overlapnum);
System.out.println("count number is:"+count);
System.out.println("the sortedlist of this term:"+sortedlist);

//check from top to bottom
for (int i=count+overlapnum+1 ;i<sortedlist.size()-count-overlapnum;i++)
{
//case1
//and
// ------
// ------
if ( a.start()>sortedlist.get(i).start() &&a.start()<sortedlist.get(i).end() && a.end()>sortedlist.get(i).end() && a.height()>sortedlist.get(i).height() && a.equals(sortedlist.get(i))==false)
{
System.out.println("hit case1");
tony.add(new Position(a.start(), sortedlist.get(i).end(), a.height()-sortedlist.get(i).height()));
a= new Position(sortedlist.get(i).end(),a.end(),a.height());
sortedlist.set(count, a);
overlapnum++;
overlaps(sortedlist);

}
//case2
//and
//------
// ------
else if(a.end()>sortedlist.get(i).start() && a.end()<sortedlist.get(i).end() &&a.start()<sortedlist.get(i).start() && a.height()>sortedlist.get(i).height() && a.equals(sortedlist.get(i))==false)
{
System.out.println("hit case2");
//System.out.println(count);
tony.add(new Position(sortedlist.get(i).start(), a.end(), a.height()-sortedlist.get(i).height()));
a=new Position(a.start(),sortedlist.get(i).start(),a.height());
sortedlist.set(count, a);
overlapnum++;
overlaps(sortedlist);
}
//case3
// -------
//-----------
//***!!!!problem: why the third time recursive never hit case3?????
else if(a.start()>=sortedlist.get(i).start() && a.end()<=sortedlist.get(i).end() && a.height()>sortedlist.get(i).height() && a.equals(sortedlist.get(i))==false)
{
System.out.println("hit case3");
tony.add(new Position(a.start(),a.end(),a.height()-sortedlist.get(i).height()));
overlapnum=0;
count++;
}
//no overlaps found, directly write propping height
else
{
System.out.println("hit else");
tony.add(new Position(a.start(),a.end(),a.height()));
overlapnum=0;
count++;

}
}
return tony;
}
return null;
}

下面的目录输出

**new recursive start!(1)
sortedlist size is:4
overlapnum is:0
count number is:0
the sortedlist of this turn:[<2.0, 5.0, 4.0>, <4.0, 7.0, 3.0>, <1.0, 3.0, 2.0>, <2.0, 4.0, 1.0>]
hit case2
**new recursive start!(2)
sortedlist size is:4
overlapnum is:1
count number is:0
the sortedlist of this turn:[<2.0, 4.0, 4.0>, <4.0, 7.0, 3.0>, <1.0, 3.0, 2.0>, <2.0, 4.0, 1.0>]
hit case1
**new recursive start!(3)
sortedlist size is:4
overlapnum is:2
count number is:0
the sortedlist of this turn:[<3.0, 4.0, 4.0>, <4.0, 7.0, 3.0>, <1.0, 3.0, 2.0>, <2.0, 4.0, 1.0>]

第三个递归应该 hit case 3 ,但是从日志结果来看,似乎没有达到任何条件,很奇怪。

最佳答案

for (int i=count+overlapnum+1 ;i<sortedlist.size()-count-overlapnum;i++)

for (int i=0+2+1 ;i<4-0-2;i++)

for (int i=3 ;i<2;i++)

3<2 为假。你的 for 永远不会运行,因此没有条件被击中。

关于Java:递归从未达到正确的条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19086547/

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