如何重新启动计时器倒计时
我正在尝试重置计时器,以便它可以从数组中提取另一个值。 有人知道解决方案吗?我的错误是说:
peat = peat= 1;
local variables referenced from an inner class must be final or effectively final
我被卡住了,我知道我需要重置计时器,但我不知道该计时器。
class MyProgram {
public static void main(String[] args) {
int peat = 0;
int cdown = 0;
Scanner input1 = new Scanner(System.in);
System.out.println("What is your name");
String personsName = input1.next();
System.out.println((personsName) + " Are you ready to impove yourself");
String motiv = input1.next();
String[] myArray = new String[4];
myArray[0] = "Keep going";
myArray[1] = "1 more rep";
myArray[2] = "you have come so far don't stop now";
myArray[3] = "Think of the progress you have made";
if (motiv.equals("yes")) {
System.out.println("Today is the day hard work begins");
} else {
System.out.println("type yes when you're ready to work");
}
Scanner input2 = new Scanner(System.in);
System.out.println("You will recive quotes throughout your workout to remind you to keep working type ok if you accept");
String confirm = input2.next();
if (confirm.equals("ok")) {
final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
final Runnable runnable = new Runnable() {
int countdown = 10;
public void run() {
System.out.println(countdown);
countdown--;
if (countdown == 0 && peat < 4) {
System.out.println(myArray[0]);
//reset count down to 10
peat = peat + 1;
} else {
scheduler.shutdown();
}
}
};
scheduler.scheduleAtFixedRate(runnable, 0, 1, TimeUnit.SECONDS);
}
while (cdown < 1) {
System.out.println(myArray[1]);
cdown = cdown + 1;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如错误消息所指出的那样,Java不允许从“ lambda”范围内修改属于外部范围的变量(匿名类的实例也创建了这样的范围)。
就您而言,为了解决此问题,您可以制作
PEAT
类变量:As the error message states, Java doesn't allow modifying variables belonging to the outer scope from within a "lambda" scope (instances of anonymous classes also create such a scope).
In your case, in order to work-around this issue, you could make
peat
a class variable:这是因为
peat
不是最终或有效的最终变量。如 documentation>当匿名类访问封闭块的本地变量时,匿名类捕获该变量。这意味着该变量的值实际上是在匿名类中复制的。
在您的情况下,您正在使用lambda表达式提供
运行
的实现。像匿名课程一样,lambda表达式被说,这意味着他们不会引入任何新的范围范围。此外,它们也可以从封闭范围中捕获变量。尽管在一个块中声明了一个匿名类,但其上下文与创建的上下文完全分离。必须防止用匿名类进行的任何修改,因为没有办法反映封闭范围中的这些更改。同样,在封闭范围中,该变量必须保持未修改,因为一旦其值被匿名类捕获,封闭范围的任何更改都不能反映到匿名类中。
如果要启动使用
scheduledthreadpoolexecutor
的计时器,则不能依靠在GO上定义的类并使用封闭范围的变量。您需要声明扩展运行
的类。在下面的示例中,可以通过传递秒数,对调度程序的引用和一系列消息来实例化计时器
类。This is because
peat
is not a final or an effectively-final variable. As the documentation states:When an anonymous class accesses a local variable of the enclosing block, the anonymous class captures that variable. Meaning that the variable's value is literally copied within the anonymous class.
In your case, you're using a lambda expression to provide an implementation of
Runnable
. Like anonymous classes, lambda expressions are said lexically scoped, meaning that they do not introduce any new level of scoping. Furthermore, they can too capture variables from the enclosing scope.Although an anonymous class is declared within a block, its context is completely dissociated from the one where it has been created. Any modification made from the anonymous class must be prevented, as there is no way to reflect those changes in the enclosing scope. Likewise, in the enclosing scope, the variable must remain unmodified, as once its value has been captured by the anonymous class, any changes from the enclosing scope cannot be reflected into the anonymous class.
If you want to launch a timer with a
ScheduledThreadPoolExecutor
, then you cannot rely on a class defined on the go and use the enclosing scope's variables. You need to declare a class extendingRunnable
. In the following example, theTimer
class can be instantiated by passing the number of seconds, a reference to the scheduler and an array of messages.