刚刚写了一个模拟红绿灯的JAVA程序,有点小问题,请各位指教!
虽然调试通过,可是总觉得有点问题。
代码:
package test;
import java.util.Timer;
import java.util.TimerTask;
public class LightTest
{
private final Timer timer = new Timer();
private final int second;
private final int color;
public LightTest( int second, int color )
{
this.second = second;
this.color = color;
}
public void DisplayLight( int color )
{
switch( color )
{
case 1:
{
System.out.println( "红灯亮 " );
break;
}
case 2:
{
System.out.println( "黄灯亮 " );
break;
}
case 3:
{
System.out.println( "绿灯亮 " );
break;
}
default:
{
System.out.println( "输入参数错误! " );
break;
}
}
}
public void start()
{
DisplayLight( color );
timer.schedule( new TimerTask()
{
public void run()
{
timer.cancel();
}
}, second*1000 );
}
public static void main(String[] args)
{
while ( true )
{
LightTest green = new LightTest( 14, 3 );
green.start();
LightTest yellow = new LightTest( 6, 2 );
yellow.start();
LightTest red = new LightTest( 20, 1 );
red.start();