[英]Timer doesn't work but giving me no errors
這是我用來運行“游戲”的類:
package glaces;
import java.util.Scanner;
import java.util.Timer;
import java.util.TimerTask;
public class Game {
public Game(){
}
public static void play(){
Scanner sc = new Scanner(System.in);
int continueLoop = 1;
final Ocean ocean = new Ocean();
int[][] tab = ocean.getColors();
int[][] tab1;
Pingouin pingouin = new Pingouin(ocean,tab);
ArcticImage img = new ArcticImage(ocean.getWidth(),ocean.getHeight());
img.setColors(tab);
TimerTask task = new TimerTask() {
public void run() {
ocean.meltIcebergs(1.);
}
};
Timer timer = new Timer();
timer.schedule(task, 3000);
while(continueLoop == 1){
System.out.println("Veuillez saisir une lettre : \n \t->X : Avancer le pingouin en bas.\n \t->Z : Avancer le pingouin en haut.\n \t->Q : Avancer le pingouin a gauche.\n \t->D : Avancer le pingouin a droite.");
String choice = sc.next();
if(choice.equals("x") || choice.equals("X")){
if(pingouin.getHeight() < ocean.getHeight()){
tab1 = pingouin.moveX();
img.setColors(tab1);
}
} else if(choice.equals("z") || choice.equals("Z")){
if(pingouin.getHeight() < ocean.getHeight()){
tab1 = pingouin.moveZ();
img.setColors(tab1);
}
} else if(choice.equals("q") || choice.equals("Q")){
if(pingouin.getWidth() < ocean.getWidth()){
tab1 = pingouin.moveQ();
img.setColors(tab1);
}
} else if(choice.equals("d") || choice.equals("D")){
if(pingouin.getWidth() < ocean.getWidth()){
tab1 = pingouin.moveD();
img.setColors(tab1);
}
} else{
System.out.println("Merci, au revoir.");
continueLoop=0;
}
}
}
public static void main(String[] args) {
play();
}
}
所以一切都可以在我的play()方法中完美地工作,但是計時器似乎不起作用,它沒有給我一個錯誤,所以我知道這不是編譯錯誤,但仍然無法執行預期的操作這里:
TimerTask task = new TimerTask() {
public void run() {
ocean.meltIcebergs(1.);
}
};
Timer timer = new Timer();
timer.schedule(task, 3000);
如果對您而言重要的是,Ocean類如下所示:
package glaces;
import geometrie.Point ;
import java.util.Random;
public class Ocean {
private Iceberg2D[] tab;
private int heightOfTable;
private int widthOfTable;
public Ocean(){
heightOfTable = 300;
widthOfTable = 300;
Random rand = new Random();
int randNumb = rand.nextInt(6); // number of iceberg
tab = new Iceberg2D[randNumb + 1];
for(int i=0; i<tab.length; i++){
int randX1 = rand.nextInt(200);
int randY1 = rand.nextInt(200);
int randX2 = rand.nextInt(21)+200;
int randY2 = rand.nextInt(21)+200;
tab[i] = new Iceberg2D(new Point(randX1,randY1),new Point(randX2,randY2));
}
}
public Ocean(int nb, int height, int width){
tab = new Iceberg2D[nb];
heightOfTable = height;
widthOfTable = width;
Random rand = new Random();
for(int i=0; i<tab.length; i++){
int randX1 = rand.nextInt(heightOfTable-1);
int randY1 = rand.nextInt(heightOfTable-1);
int randX2 = rand.nextInt(widthOfTable-randX1)+(randX1);
int randY2 = rand.nextInt(widthOfTable-randY1)+(randY1);
tab[i] = new Iceberg2D(new Point(randX1,randY1),new Point(randX2,randY2));
}
}
public int getWidth(){
return heightOfTable;
}
public int getHeight(){
return widthOfTable;
}
public int getCount(){
return tab.length;
}
public void meltIcebergs(double fr){
for(int i = 0; i<tab.length; i++){
tab[i].meltIcebergs(fr);
}
}
public int[][] getColors(){
int i,j,k;
int[][] table = new int[heightOfTable][widthOfTable];
for(i=0; i<table.length; i++){
for(j=0; j<table.length; j++){
table[i][j] = 0;
}
}
for(i=0; i<tab.length; i++){
int w1 = (int) this.tab[i].cornerBottomLeft().getAbscisse();
int w2 = (int) this.tab[i].cornerTopRight().getAbscisse();
int h1 = (int) this.tab[i].cornerBottomLeft().getOrdonnee();
int h2 = (int) this.tab[i].coinEnHautADroite().getOrdonnee();
for(k=0; k<table.length; k++){
for(j=0; j<table.length; j++){
if(k > w1 &&
k < w2 &&
j > h1 &&
j < h2
){
table[k][j] = 1;
}
}
}
}
return table;
}
public String toString(){
return "Il y a "+getCount()+" iceberg | L'hauteur de l'ocean est "+getHeight()+" et la largeur de l'ocean est " + getWidth();
}
public static void main(String[] args) {
Ocean ocean = new Ocean(2,700,700);
int[][] i = ocean.getColors();
ArcticImage img = new ArcticImage(700,700);
img.setColors(i);
}
}
我想你需要換個電話
timer.schedule(task, 3000);
至
timer.schedule(task, 3000, 3000);
第一個僅在指定的持續時間調用TimerTask一次,第二個在指定的持續時間調用TimerTask。
我也認為您應該將計時器更改為與電話惡魔
new Timer(true)
根據Javadoc,deamon是
如果計時器將用於安排重復的“維護活動”,則必須調用守護程序線程,只要應用程序正在運行,就必須執行該線程
PS另外,將名稱設置為計時器也很有用-有助於調試。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.