无法停止我的自定义时钟
Can't stop my custom clock
我创建了 class 时钟:
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
class 中,我调用 clock.start()
并且它工作正常,但是当我想停止打印时间时,我无法 that.I 假设问题是 start()
方法中的 while 循环。
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()
方法也不起作用。
reset()
方法可能有效,但在您当前的代码中,它永远不会达到
Scanner input = new Scanner(System.in);
行,因为您首先调用了具有无限循环的 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();
}
你的 Clock
class 没问题你需要在 main
方法中使用 Thread
这样的方法
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();
}
干杯!
我创建了 class 时钟:
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
class 中,我调用 clock.start()
并且它工作正常,但是当我想停止打印时间时,我无法 that.I 假设问题是 start()
方法中的 while 循环。
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()
方法也不起作用。
reset()
方法可能有效,但在您当前的代码中,它永远不会达到
Scanner input = new Scanner(System.in);
行,因为您首先调用了具有无限循环的 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();
}
你的 Clock
class 没问题你需要在 main
方法中使用 Thread
这样的方法
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();
}
干杯!