下面的代码有没有简写的方法?通常在游戏中我们希望确保某些东西不会离开边界,或者更一般地说,我们希望阻止数组的索引超出数组的边界。我一直都是这样写的,但我想知道在 Actionscript、Java 或 C# 中是否有速记
在 ActionScript 中:
index++;
if (index > array.length - 1) index = array.length - 1;
据我所知,没有任何运算符可以完成此操作,但也许我错了。我知道三元运算符类似于 if (condition) ?真值:假值
最佳答案
你可以使用 Math.min
:
index = Math.min (index+1, array.length-1);
关于java - Actionscript 中 "If greater than, then equal to"的简写?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27726221/