【发布时间】:2013-01-22 22:40:30
【问题描述】:
我正在尝试使我的 Hailstone 序列输出由于 int 的限制而无法计算的最小整数,但由于某种原因它仍然无法正常工作。任何关于为什么不这样做的想法将不胜感激。
public static void main(String[] args) {
int x=2;
int count = x;
//Collatz Conjecture computation
while (true)
{ x=2;
x =count;
while (x != 1)
{
if (x % 2 == 0)
x = x / 2;
if (x % 2 == 1)
x = x * 3 + 1;
if (x < 0)
{ System.out.print("The integer " + count + " cannot have its Hailstone sequence computed using int variables. ");
return;
}
}
count ++;
}
}
【问题讨论】:
-
到底是怎么回事?
-
当我运行程序时它没有输出任何东西。即使我将它设置为一旦找到就输出该整数值。
-
拿出笔和纸,像计算机一样逐步完成程序。写下每个变量和分配给它们的值。 x 是否小于 0?
-
当出现溢出错误时,它从 int 限制的负整数范围开始。