对于以下代码,当“n”大约100,000时停止运行。我需要它运行到100万。我不知道它出错的地方,我仍然在学习Java,所以在代码中也可能存在一些简单的错误。循环停止运行java
public class Problem14{
public static void main(String[] args) {
int chainLength;
int longestChain = 0;
int startingNumber = 0;
for(int n =2; n<=1000000; n++)
{
chainLength = getChain(n);
if(chainLength > longestChain)
{
System.out.println("chainLength: "+chainLength+" start: "+n);
longestChain = chainLength;
startingNumber = n;
}
}
System.out.println("longest:"+longestChain +" "+"start:"+startingNumber);
}
public static int getChain(int y)
{
int count = 0;
while(y != 1)
{
if((y%2) == 0)
{
y = y/2;
}
else{
y = (3*y) + 1;
}
count = count + 1;
}
return count;
}
}
我只是好奇 - 一般来说代码的目的是什么? – Coffee 2012-08-17 15:39:04
您是否尝试捕获异常以查看是否抛出了未处理的异常?也许堆栈溢出? – 2012-08-17 15:40:11
我在做Euler项目,它是一个网站,你必须解决数学问题。 继承人我在做的问题:http://projecteuler.net/problem=14 – stackErr 2012-08-17 15:41:41