日期:2014-05-20  浏览次数:20619 次

高手赐教,一个小问题
我现在又有一个问题,改进以下线程,使它能够在屏幕上循环打印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;
}
这样应该没问题了。