为什么我的计时器不会每秒都打印?

huangapple go评论99阅读模式
英文:

Why doesn't my timer print every second that passes?

问题

I wanted to make a code that printed every second that passes in my timer for a total of 30 seconds (hence why I made a for loop) however it just repeatedly prints 1, so I am guessing that my for loop isn't working and it isn't appending 1 to the variable score. Any suggestions on what I should do? Thanks.

我想制作一个代码,每秒打印一次,总共打印30秒(所以我使用了一个for循环),但它只不断地打印1,所以我猜我的for循环不起作用,也没有将1追加到变量score。有什么建议吗?谢谢。

英文:

I wanted to make a code that printed every second that passes in my timer for a total of 30 seconds (hence why I made a for loop) however it just repeatedly prints 1, so I am guessing that my for loop isn't working and it isn't appending 1 to the variable score. Any suggestions on what I should do? Thanks.

public class TimerSchedule {

public static void main(String[] args) {  
	// creating timer task, timer  
	Timer t = new Timer();  
	TimerTask tt = new TimerTask() {  
	    @Override  
	    public void run() {  
	       for(int i=0; i<30;i++)  
	        {  
	        int score = 0;
	        score ++;
	        System.out.println(score);
	        }  
	    };  
	};  
	t.scheduleAtFixedRate(tt,0,1000);    
	   }  
	}  

答案1

得分: 0

打印 1 的原因是因为你将以下两个语句放在了 for 循环内部:

int score = 0;
System.out.println(score);

第一个语句会在每次迭代中重置 score,而第二个语句会在每次迭代中打印更新后的 score 值。第一个语句应该放在 run() 外部。此外,当 score 达到 30 时,你需要取消计时器。

下面是更新后的代码:

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

public class Main {
    public static void main(String[] args) {
        Timer t = new Timer();
        TimerTask tt = new TimerTask() {
            int score = 0;

            @Override
            public void run() {
                System.out.println(++score);
                if (score == 30) {
                    t.cancel();
                }
            };
        };
        t.scheduleAtFixedRate(tt, 0, 1000);
    }
}

输出结果:

1
2
3
...
...
...
29
30
英文:

The reason why it is printing 1 is that you have put the following two statements inside the for loop:

int score = 0;
System.out.println(score);

The first one is resetting score in every iteration while the second one prints the updated value of score in each iteration. The first one should be put outside run(). Also, you need to cancel the timer when the value of score reaches 30.

Given below is the updated code:

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

public class Main {
	public static void main(String[] args) {
		Timer t = new Timer();
		TimerTask tt = new TimerTask() {
			int score = 0;

			@Override
			public void run() {
				System.out.println(++score);
				if (score == 30) {
					t.cancel();
				}
			};
		};
		t.scheduleAtFixedRate(tt, 0, 1000);
	}
}

Output:

1
2
3
...
...
...
29
30

huangapple
  • 本文由 发表于 2020年8月2日 03:12:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/63209113.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定