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

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

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

问题

我正在尝试制作一个Java井字棋游戏但代码有一些问题

我想要做的是在用户点击按钮时只是对一个整数进行递增操作

问题是编译器报错:`"Error:local variables referenced from a lambda expression must be final or effectively final"`。我理解这句话的意思是我只能创建一个值而不能在同一个方法中重新定义它但我对Java还不熟悉不知道有什么解决方法

我正在考虑通过使用多个类来解决但这太复杂了我只想像在Python中那样对整数进行累加如果有人知道如何做或者有解决方法我会非常感激

以下是我的程序

```java
public class TicTacToe extends Application {

    @Override
    public void start(Stage primaryStage) {
        //一些代码在这里

        //这里创建了所有按钮
        //按钮以3X3的数组格式排列,共9个按钮
        Button B1 = new Button();
        //更多按钮

        //这里设置了按钮的大小

        //这是跟踪用户点击了哪个按钮的整数
        //这是我的值
        int User = 0;

        //这里获取用户是否点击了任何按钮,理论上应该执行某些操作
        B1.setOnAction((event) -> {
            B1.setText("X");
            //这是我想要对值进行累加的地方
            User++;
        });
        
        //更多代码

        System.out.println(User);
    }

}
英文:

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

public class TicTacToe extends Application {

    @Override
    public void start(Stage primaryStage) {
        //Some code here

        //this is Cerateing all the buttons
        //the buttons are formated in a 3X3 array for a total of 9 buttons
        Button B1 = new Button();
        //More buttons

        //this sets the size of the button

        //this is the int that tracks what button the user has pressed
        //this is my value
        int User = 0;

        //this gets if the user has clicked any of the buttons shold in therory do somthing
        B1.setOnAction((event) -> {
            B1.setText("X");
            //this is where I want to add to an value
            User++;
        });
        
        //More code

        System.out.println(User);
    }

}

答案1

得分: 1

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

int User = 0;

B1.setOnAction((event) -> {
    B1.setText("X");
    User++;
});

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

private int User = 0;

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

int User = new int[]{0};

B1.setOnAction((event) -> {
    B1.setText("X");
    User[0]++;
});

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

AtomicInteger User = new AtomicInteger(0);

B1.setOnAction((event) -> {
    B1.setText("X");
    User.incrementAndGet();
});
英文:

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

int User = 0;

B1.setOnAction((event) -> {
    B1.setText("X");
    User++;
});

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.

private int User = 0;

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

int User = new int[]{0};

B1.setOnAction((event) -> {
    B1.setText("X");
    User[0] ++;
});

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:

AtomicInteger User = new AtomicInteger(0);

B1.setOnAction((event) -> {
    B1.setText("X");
    User.incrementAndGet();
});

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:

确定