What does the percentage symbol (%) mean?
本问题已经有最佳答案,请猛点这里访问。
我遇到了一些在数组参数中包含
这是什么意思,如何工作?
示例:
1 2 3 4 5 |
输出:
1 2 3 | a d c |
这是余数运算符,它给出整数除法的余数。例如,
在那里使用它来保持范围内的值:如果
这样,即使数字(4、7或50)超出范围,代码也可以可靠地从数组中选择条目。
的有效数组索引。
完整示例(活动副本):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | class Example { public static void main (String[] args) throws java.lang.Exception { String[] name = {"a" ,"b" ,"c" ,"d"}; int n; n = 4 % name.length; System.out.println(" 4 % 4 is" + n +":" + name[n]); n = 7 % name.length; System.out.println(" 7 % 4 is" + n +":" + name[n]); n = 50 % name.length; System.out.println("50 % 4 is" + n +":" + name[n]); } } |
输出:
1 2 3 | 4 % 4 is 0: a 7 % 4 is 3: d 50 % 4 is 2: c |
简单:这是模,或者精确地说是余数运算符。
这与数组本身无关。这只是对用于计算数组索引的值的数值计算。