我有一个程序(用Java编写),当它到达for或while循环时,它将停顿大约2秒钟,然后完全停止。这是代码:
import javax.swing.*;
public class numberCruncher
{
public static int number,guess,x;
public static void main(String[] argv)
{
number=enterIntGUI("Enter a number for the\ncomputer to crack\n(5 digits maximum):");
System.out.print("1");
test();
}
public static void test()
{
boolean correct = false;
System.out.print("2");
//while(correct=false)
for(x=0;x>999999999;x++)
{
//x++;
System.out.print("3");
guess=cleanUsage.random(0,99999);
System.out.print("4");
System.out.println(" "+guess);
if (guess==number)
{
System.out.println();
System.out.println("Correct number guessed in "+x+" tries");
//correct = true;
}
}
}
public static int enterIntGUI(String prompt)
{
String tempString = JOptionPane.showInputDialog(prompt);
int temp = Integer.parseInt(tempString);
return temp;
}
}
您可以看到我对while循环有评论。另外,我在我认为会被卡住的地方放入了4个println语句,并分别打印了1和2,但没有打印3。这是我的cleanUsage类的一部分,其中包含随机数生成器:
public static int random(int min, int max)
{
int range = max - min + 1;
int number = (int) (range * Math.random() + min);
return number;
}
我问过其他一些人,他们无法弄清楚。如果您能帮助我,那将很棒。
它不会打印,3
或者4
因为条件x>999999999
是最初的,false
所以for
永远不会进入循环。我想你是故意的<
。
另请注意,您可以使用break
而不是correct = true
退出循环。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句