class Counter
{
private static int number;
private int m;
public Counter()
{
m++;number++;
System.out.println("This is NO."+number+" object,m="+m);
}
}
public class Staticstics
{
public static void main(String[] args)
{
for(int i=10;i<Math.random()*10;i++)
new Counter();
}
}
程序编译、运行都没问题,可怎么看不到结果啊
{
private static int number;
private int m;
public Counter()
{
m++;number++;
System.out.println("This is NO."+number+" object,m="+m);
}
}
public class Staticstics
{
public static void main(String[] args)
{
for(int i=10;i<Math.random()*10;i++)
new Counter();
}
}
程序编译、运行都没问题,可怎么看不到结果啊
i永远都大于Math.random()*10
new Counter(); 也就永远不会执行了
所以,上述代码中的循环条件一直是不成立的,故循环未执行...
i<(int)(Math.random()*10)
循环条件不成立
for(int i=10;i <Math.random()*10;i++)
改为:
for(int i=0;i <Math.random()*10;i++)
for(int i=10;i <Math.random()*10;i++)
改为:
for(int i=0;i <10;i++)
更简单,易于测试