Java中的int无法更改,因为lambda表达式必须是final的或者是有效final的。

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

Java int can't be changed because lambda expression must be final or effectively final

问题

  1. 我正在尝试制作一个Java井字棋游戏但代码有一些问题
  2. 我想要做的是在用户点击按钮时只是对一个整数进行递增操作
  3. 问题是编译器报错`"Error:local variables referenced from a lambda expression must be final or effectively final"`我理解这句话的意思是我只能创建一个值而不能在同一个方法中重新定义它但我对Java还不熟悉不知道有什么解决方法
  4. 我正在考虑通过使用多个类来解决但这太复杂了我只想像在Python中那样对整数进行累加如果有人知道如何做或者有解决方法我会非常感激
  5. 以下是我的程序
  6. ```java
  7. public class TicTacToe extends Application {
  8. @Override
  9. public void start(Stage primaryStage) {
  10. //一些代码在这里
  11. //这里创建了所有按钮
  12. //按钮以3X3的数组格式排列,共9个按钮
  13. Button B1 = new Button();
  14. //更多按钮
  15. //这里设置了按钮的大小
  16. //这是跟踪用户点击了哪个按钮的整数
  17. //这是我的值
  18. int User = 0;
  19. //这里获取用户是否点击了任何按钮,理论上应该执行某些操作
  20. B1.setOnAction((event) -> {
  21. B1.setText("X");
  22. //这是我想要对值进行累加的地方
  23. User++;
  24. });
  25. //更多代码
  26. System.out.println(User);
  27. }
  28. }
英文:

I am trying to make a Java tic tac toe game and am having some issues with the code.

What I want to do is just increment an int when the user clicks a button.

The problem is is that the compiler says "Error:local variables referenced from a lambda expression must be final or effectively final". What I understand this to mean is that I can only create an value and not redefine it in the same method but I am new to java and have no idea for a work around.

I am thinking of a work around by having multiple classes but this is overly complicated and I just want to add to the int like in python. If anyone has any idea on how to do this or a work around then it would be greatly appreciated.

Below is my program

  1. public class TicTacToe extends Application {
  2. @Override
  3. public void start(Stage primaryStage) {
  4. //Some code here
  5. //this is Cerateing all the buttons
  6. //the buttons are formated in a 3X3 array for a total of 9 buttons
  7. Button B1 = new Button();
  8. //More buttons
  9. //this sets the size of the button
  10. //this is the int that tracks what button the user has pressed
  11. //this is my value
  12. int User = 0;
  13. //this gets if the user has clicked any of the buttons shold in therory do somthing
  14. B1.setOnAction((event) -> {
  15. B1.setText("X");
  16. //this is where I want to add to an value
  17. User++;
  18. });
  19. //More code
  20. System.out.println(User);
  21. }
  22. }

答案1

得分: 1

你没有指明你在哪里出错,但是这部分代码应该会产生一个错误:

  1. int User = 0;
  2. B1.setOnAction((event) -> {
  3. B1.setText("X");
  4. User++;
  5. });

User 变量需要是一个字段,一个类似于 AtomicIntegernew int[1] 的包装器,或者是一个 final 对象。以下是几种解决方法。首先,你可以将 User 声明为一个字段。

  1. private int User = 0;

然后就不要在方法中再声明它了。另一种方法是创建一个 new int[1]

  1. int User = new int[]{0};
  2. B1.setOnAction((event) -> {
  3. B1.setText("X");
  4. User[0]++;
  5. });

然后在访问它时,你可以使用 User[0]。另一种方式是使用 AtomicInteger。我个人不是特别喜欢这种方法,但是你可以这样使用:

  1. AtomicInteger User = new AtomicInteger(0);
  2. B1.setOnAction((event) -> {
  3. B1.setText("X");
  4. User.incrementAndGet();
  5. });
英文:

You didn't specify where you got error, but this part should give an error:

  1. int User = 0;
  2. B1.setOnAction((event) -> {
  3. B1.setText("X");
  4. User++;
  5. });

The User variable either needs to be a field, a wrapper like AtomicInteger or new int[1], or a final object. Here are a couple of ways to get around this. First of all, you can just make User a field.

  1. private int User = 0;

Then don't declare it in the method. Another thing you can do is make a new int[1].

  1. int User = new int[]{0};
  2. B1.setOnAction((event) -> {
  3. B1.setText("X");
  4. User[0] ++;
  5. });

Then when accessing it, you can use User[0]. Another way is using AtomicInteger. I don't really like it that much, but here is how you can use it anyways:

  1. AtomicInteger User = new AtomicInteger(0);
  2. B1.setOnAction((event) -> {
  3. B1.setText("X");
  4. User.incrementAndGet();
  5. });

huangapple
  • 本文由 发表于 2020年5月4日 06:59:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/61582758.html
匿名

发表评论

匿名网友

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

确定