无法停止我的自定义时钟

时间:2015-07-16 11:12:00

标签: java

我创建了名为Clock的课程:

import java.util.concurrent.TimeUnit;

  public class Clock {

    private int seconds;
    private int minutes;
    private int hours;
    private String printableSeconds;
    private String printableMinutes;
    private String printableHours;
    public boolean shouldStop;
    public Clock() {}
    public void start() {
        shouldStop = false;
        while (shouldStop == false) {
            wait(1000);
            icrementSeconds();
            printableSeconds = "" + seconds;
            if (seconds < 10) addZeroBeforeSeconds();
            printableMinutes = "" + minutes;
            if (minutes < 10) addZeroBeforeMinutes();
            printableHours = "" + hours;
            if (hours < 10) addZeroBeforeHours();
            if (seconds == 60) {
                icrementMinutes();
            }
            if (minutes == 60) {
                icrementHours();
            }
            printTime();
            System.out.println(shouldStop);
        }
    }
    public void stop() {
        shouldStop = true;
    }
    public void reset() {
        System.out.println("RESETING");
        seconds = 0;
        minutes = 0;
        hours = 0;
    }
    private void wait(int ms) {
        try {
            TimeUnit.MILLISECONDS.sleep(ms);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }
    private void addZeroBeforeSeconds() {
        printableSeconds = "0" + seconds;
    }
    private void addZeroBeforeMinutes() {
        printableMinutes = "0" + minutes;
    }
    private void addZeroBeforeHours() {
        printableHours = "0" + hours;
    }
    private void icrementSeconds() {
        seconds++;
    }
    private void icrementMinutes() {
        seconds = 0;
        minutes++;
    }
    private void icrementHours() {
        minutes = 0;
        hours++;
    }
    private void printTime() {
        System.out.println(printableHours + ":" + printableMinutes + ":" + printableSeconds);
    }
 }

在我的Main课程中,我打电话给clock.start()并且它工作正常,但是当我想要停止打印时间时,我只是无法做到这一点。我认为问题是那个while start()方法中的循环。

   import java.util.Scanner;
   import java.util.concurrent.TimeUnit;


    public class Main {

    public static void main(String[] args) {


        Clock clock = new Clock();
         clock.start();

         Scanner input = new Scanner(System.in);
         int a = input.nextInt();

            if(a==1)
            clock.stop();

    }

}

例如,我想按1停止时钟。reset()方法也不起作用。

2 个答案:

答案 0 :(得分:0)

reset()方法可能有效,但在您当前的代码中,它永远不会到达

Scanner input = new Scanner(System.in);

Line,因为您首先调用具有无限循环的clock.start();方法。因此,一旦该循环退出,您就可以调用用户输入。

解决方法是创建一个线程。在您的主题中,请致电clock.start();,现在您的main方法将继续执行,现在您可以要求用户输入并致电clock.stop();

一些示例代码(手工编写未经测试):

public class Clock implements Runnable{

    private int seconds;
    private int minutes;
    private int hours;
    private String printableSeconds;
    private String printableMinutes;
    private String printableHours;
    public boolean shouldStop;
    public Clock() {}

    public void run() {   // Notice how I changed this to run
        shouldStop = false;
        while (shouldStop == false) {
            wait(1000);
            icrementSeconds();
            printableSeconds = "" + seconds;
            if (seconds < 10) addZeroBeforeSeconds();
            printableMinutes = "" + minutes;
            if (minutes < 10) addZeroBeforeMinutes();
            printableHours = "" + hours;
            if (hours < 10) addZeroBeforeHours();
            if (seconds == 60) {
                icrementMinutes();
            }
            if (minutes == 60) {
                icrementHours();
            }
            printTime();
            System.out.println(shouldStop);
        }
    }
    public void stop() {
        shouldStop = true;
    }

    //yourcode
}

main()

public static void main(String[] args) {


    Clock clock = new Clock();
    Thread th = new Thread(clock);
    th.start();

    Scanner input = new Scanner(System.in);
    int a = input.nextInt();

    if(a==1)
        clock.stop();    
}

答案 1 :(得分:0)

您的Clock课程没问题,您需要在Thread方法中使用main

  final Clock clock = new Clock();

  Thread t = new Thread(new Runnable() {
    public void run() {
        clock.start();
    }
  });

  t.start();

  Scanner input = new Scanner(System.in);
  int a = input.nextInt();

  if(a==1){
     t.stop();
  }

喝彩!