5

私はJavaFXが初めてです。以下のコードが機能しない理由を理解できません。

import javafx.util.Sequences;

def nums = [1..10];
var curr = 0;

var evenOrOdd = bind if (nums[curr] mod 2 == 0) "{nums[curr]} is an even number" else "{nums[curr]} is an odd number";

for (curr in [0..(sizeof nums -1)])
{
    println("{evenOrOdd}");
}

私は得ています

1 is an odd number
1 is an odd number
1 is an odd number
1 is an odd number
1 is an odd number
1 is an odd number
1 is an odd number
1 is an odd number
1 is an odd number
1 is an odd number

コードを次のように変更すると

import javafx.util.Sequences;

def nums = [1..10];
var curr = 0;

var evenOrOdd = bind if (nums[curr] mod 2 == 0) "{nums[curr]} is an even number" else "{nums[curr]} is an odd number";

for (i in [0..(sizeof nums -1)])
{
    curr = i;
    println("{evenOrOdd}");
}

正しい出力が得られます:

1 is an odd number
2 is an even number
3 is an odd number
4 is an even number
5 is an odd number
6 is an even number
7 is an odd number
8 is an even number
9 is an odd number
10 is an even number

明らかに、ループ内のカウンターの増分は値の変更として扱われず、バインドされた式は再評価されません。

誰でもこの動作の背後にある概念を説明できますか?

4

1 に答える 1