gpt4 book ai didi

java - 如何创建像电话一样的秒/毫秒计时器?

转载 作者:太空宇宙 更新时间:2023-11-04 10:23:10 25 4
gpt4 key购买 nike

我对 Java 编码比较陌生,可以被视为新手。我还有很多东西要学,但是,我试图创建一个带有倒计时和秒表的迷你乘法游戏。我的愿望是秒表在倒计时说“开始!”时以秒和毫秒为单位进行计数。我想显示玩家解决问题花了多长时间。我尝试查找有关如何初始化秒表的教程,但是它似乎没有初始化。我知道这一点是因为我打印了值,SecondPassed 最后显示为 0。任何人都可以帮我修复我的代码,以便我可以在“Go!”之后的几秒和几毫秒内启动计时器。并在游戏结束后显示时间?谢谢。

import java.util.Scanner;
import java.util.Timer;
import java.util.TimerTask;

public class Mathgame {


public static int secondsPassed = 0;

Timer timer = new Timer();
TimerTask task = new TimerTask() {

public void run() {

secondsPassed++;

}

};


public void start() {


timer.scheduleAtFixedRate(task,1000,1000);
}

public static void main(String[] args) {



Scanner input = new Scanner(System.in);

System.out.println("Welcome to 'The Math Game!' Please chose whether you would like to play or not by inputting 'yes' or 'no'.");

String x = input.nextLine();




if(x.equals ("no")) {
System.out.println("Ok! Maybe next time!");

System.exit(1);
} else {

System.out.println("OK! We are starting the game in...");

System.out.println("3");

try {
Thread.sleep(1000);
} catch(InterruptedException ex){

System.exit(0);
}

System.out.println("2");
try {
Thread.sleep(1000);

} catch(InterruptedException ex) {
System.exit(0);
}

System.out.println("1!!");

try {
Thread.sleep(1000);

} catch(InterruptedException ex) {
System.exit(0);
}


System.out.println("Go!");

System.out.println("Your timer started!");



System.out.println(secondsPassed);


}

}

}

最佳答案

我不知道你想做什么,但我对你的代码做了一些修改,这使它变得更加有趣和干净......

public class Mathgame {

// it is not necessary do it static
public int secondsPassed = 0;

//alternatively you can use Timer timer = new Timer(true); if you want to stop timer immediately after method main is finished
Timer timer = new Timer();
TimerTask task = new TimerTask() {

public void run() {

secondsPassed++;
// your timer will run this method every 1 sec, so you need to put output here if you want to see changes
System.out.println("Running my task again. secondsPassed is " +secondsPassed);
}

};

public void start() {
//this timmer will run your task every 1000ms (1 second) and will start in 1000ms (1 second) after start is called
timer.scheduleAtFixedRate(task, 1000, 1000);
}

// this is main method so it is better to throw exceptions upper then handler them without output
public static void main(String[] args) throws InterruptedException {


Scanner input = new Scanner(System.in);

System.out.println("Welcome to 'The Math Game!' Please chose whether you would like to play or not by inputting 'yes' or 'no'.");

String x = input.nextLine();

// it is better to check for 'yes' than for 'no' this is good practice
if (x.equalsIgnoreCase("yes") || x.equalsIgnoreCase("y")) {

System.out.println("OK! We are starting the game in...");

System.out.println("3");

Thread.sleep(1000);

System.out.println("2");

Thread.sleep(1000);

System.out.println("1!!");

Thread.sleep(1000);

System.out.println("Go!");

//starting your timer, we have to create object of your class first and then run method start
Mathgame mathgame = new Mathgame();
mathgame.start();

System.out.println("Your timer started!");

//main is finished however there is timer thread which is still working
// if you want to stop timer immediately after main then you need to build it as 'Timer timer = new Timer(true);'
// in that case you may put here extra sleep and see how timer is working

//let wait until timer will count 10 sec
Thread.sleep(10000);
//you can check secondsPassed from main too
System.out.println("This is called from main. secondsPassed value is " + mathgame.secondsPassed);

System.out.println("Method main is finished");
}
}
}

关于java - 如何创建像电话一样的秒/毫秒计时器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50862409/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com