JavaFX绑定/循环问题

时间:2009-04-11 04:01:14

标签: java javafx

好的,我正在使用Passion课程学习JavaFX并遇到一个我似乎无法弄清楚的问题。

这与此问题相似 -

Why doesn't this "binding" code work as expected in JavaFX?

def numbers = [1..10];
var currentNumber = 0;    

for (currentNumber in numbers){
var evenOrOdd = bind if (numbers[currentNumber] mod 2 == 0)
    "----{numbers[currentNumber]} is an even number"
    else "----{numbers[currentNumber]} is an odd number";
println("{evenOrOdd}");
}

和我的输出 -

----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
----0 is an even number

我遇到的问题是它“看起来”运行正确,除了它不显示1作为奇数,然后它在10之后添加0我花了一些时间单步执行程序并且可以'似乎要弄清楚它是如何/为什么这样做的。

我可以让它在没有任何约束的情况下运行良好,所以不确定这是否是导致我的问题。

任何想法/想法?

〜阿伦

1 个答案:

答案 0 :(得分:3)

currentNumber是数字的元素之一,而不是索引

var evenOrOdd = bind if (currentNumber mod 2 == 0)
    "----{currentNumber} is an even number"
    else "----{currentNumber} is an odd number";