下面是我用来运行“游戏”的类:
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);
如果对你来说很重要的话,海洋类如下所示:
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);
}
}
发布于 2016-04-19 11:07:14
我想你应该换个电话
timer.schedule(task, 3000);
至
timer.schedule(task, 3000, 3000);
第一个函数只调用TimerTask一次,第二个函数在指定的持续时间内调用TimerTask。
另外,我认为你应该把你的计时器改成守护呼叫
new Timer(true)
根据Javadoc的说法,守护进程
如果定时器将用于安排重复的“维护活动”,则调用
守护进程线程,只要应用程序正在运行,就必须执行这些活动
另外,设置定时器的名称也很有用--它可以帮助你进行调试。
https://stackoverflow.com/questions/35208604
复制