我有一个游戏,包括随着游戏进行的计时,我的意思是你玩游戏和时间滴答,当它完成时它会停止游戏。但我不能一次运行两个代码线程......有什么办法可以解决这个问题吗?
这是我的计时器代码:
public class Time {
private static String gl;
private static int gli;
public static int weeks = 0;
public static void main(String[] args) throws InterruptedException {
startdays();
}
public static void startdays() throws InterruptedException {
gl = JOptionPane.showInputDialog("How many weeks? do you want the game to to on for?\nPlease type numbers, Or game will crash.\nEach week is 10 seconds long.");
gli = Integer.parseInt(gl);
gli++;
for (int i = 0; i < gli; i++) {
if(weeks < gli){
Thread.sleep(10000);
weeks++;
}else if(weeks == gli){
JOptionPane.showMessageDialog(null, "Uh oh, Your time in office is up. You died of an infectuous disease.");
}
}
}
}
在另一个类中,我这样调用该方法
JOptionPane.showMessageDialog(frame, compname);
Time time = new Time();
time.startdays();
JOptionPane.showMessageDialog(frame, "Im still here");
那么有没有办法让计时器和游戏同时进行呢?