首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

高手赐教,一个小疑点

2012-01-24 
高手赐教,一个小问题我现在又有一个问题,改进以下线程,使它能够在屏幕上循环打印0--9之间的数字呢?classMu

高手赐教,一个小问题
我现在又有一个问题,改进以下线程,使它能够在屏幕上循环打印0--9之间的数字呢?
        class   MuilThread   implements   Runnable
{
public   static   void   main(String[]   args)
{                       //static   int   i=0;
                    MuilThread   mt=new   MuilThread();
                    Thread   t1=new   Thread(mt);
                    Thread   t2=new   Thread(mt);
                  System.out.println( "线程t1启动 ");  
t1.start();
try
{
Thread.sleep(3000);
}
    catch(Exception   e)
{
e.printStackTrace();

}
System.out.println( "线程t2启动 ");
t2.start();


   
}
public   void   run()
{

int   count=10;
for(   int   i=0;i <count;i++)
{
System.out.println(i);

}
count=++count%10;


}


}



[解决办法]
class MuilThread implements Runnable
{
public static void main(String[] args)
{ //static int i=0;
MuilThread mt=new MuilThread();
Thread t1=new Thread(mt);
Thread t2=new Thread(mt);
System.out.println( "线程t1启动 ");
t1.start();
try
{
Thread.sleep(3000);
}
catch(Exception e)
{
e.printStackTrace();
}
System.out.println( "线程t2启动 ");
t2.start();
}
public void run()
{
int i=0;
while(true)
{
System.out.println(Math.abs(i%10));
try{Thread.sleep(1000);}catch(Exception e){}
i++;
}
}
}
[解决办法]
for( int i=0;i <count;i++)
{
System.out.println(i);

}
count=++count%10;

貌似你的括号括错地方了。

for( int i=0;i <count;i++)
{
System.out.println(i);

count=++count%10;
}
这样应该没问题了。

热点排行