你使用的是 if(!isQuit)所以,执行一次 if 判断之后,整个线程就结束了,不会继续执行下去,不要用 if,用 while 吧,,,然后倒计时结束以后 break 掉 while 循环就好了

解决方案 »

  1.   

    你这里Thread t1貌似没有任何用处,你改成tb就可以了
      

  2.   

    回1#
    昨天自己又调了一下,将run()函数放入myThread对象的构造函数中了,这样就不用在主线程中出现run()了,因为我用run()的时候发现main函数由于是顺序执行的,若是先搞输入,那么run()就不会执行,而将run()函数放在前面,那么那个Boolean类型变量isQuit总是无法起作用,昨晚又搞了下,最后能跑了,程序如下public class TimeBomb{
    public static void main(String args[]){
    myThread mt = new myThread();
    //TimeBomb tb = new TimeBomb();
    //String line=null;
    //mt.run();
    //boolean  test = false;
    String line = null;
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    //mt.run();
    try{
    while(true){
    System.out.println("请输入密码");
    line = br.readLine();
    if(line.equals("quit")){
    mt.stoprun();
    break;
    }
    else{
    System.out.println("密码错误");
    }
    }
    }
    catch(Exception e){}

    // test = tb.quitTest();
    //mt.run();
    //if(test){
    // mt.stoprun();
    //}


    }
    public boolean quitTest(){
    String line = null;
    boolean test = false;
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    try{
    while(true){
    System.out.println("请输入密码");
    line = br.readLine();
    //return line;
    if(line.equals("quit")){
    test = true;
    break;
    }
    else{
    System.out.println("密码错误");
    }
    }
    }
    catch(Exception e){
    e.printStackTrace();
    }
    return test;
    }
    public static class myThread extends Thread{
    int n;
    boolean testbomb;
    //start();
    public void run(){
    try{
    boolean test = false;
    //if(!testbomb){
    for(int i = 0;i<n;i++){
    if(!testbomb){
    Thread.sleep(10000);
    System.out.println("距离爆炸还有 "+(6-i)+" 十秒");
    }
    //else if(i==n){
    // System.out.println("炸弹爆炸!");
    //}
    else{
    System.out.println("解除威胁");
    test = true;
    break;
    }

    }
    if(!test){
    System.out.println("炸弹爆炸!");
    }
    //System.out.println("炸弹爆炸!");
    //}
    //else{
    //System.out.println("解除威胁");
    //}
    }
    catch(Exception e){}
    }
    public void stoprun(){
    testbomb = true;
    }
    public myThread(){
      n = 6;
      testbomb = false;
    start();
    }
    }

    }