excerpt from Thinking in java 2e:
The comma operator
Earlier in this chapter I stated that the comma operator (not the comma separator, which is used to separate definitions and function arguments) has only one use in Java: in the control expression of a for loop. In both the initialization and step portions of the control expression you can have a number of statements separated by commas, and those statements will be evaluated sequentially.
//: c03:CommaOperator.java
public class CommaOperator {
public static void main(String[] args) {
for(int i = 1, j = i + 10; i < 5;
i++, j = i * 2) {
System.out.println("i= " + i + " j= " + j);
}
}
} ///:~
>>> 在 ? : 中也可以用 "," 吗? k=(k>0)?(p++, k=0):k+1;
The comma operator
Earlier in this chapter I stated that the comma operator (not the comma separator, which is used to separate definitions and function arguments) has only one use in Java: in the control expression of a for loop. In both the initialization and step portions of the control expression you can have a number of statements separated by commas, and those statements will be evaluated sequentially.
//: c03:CommaOperator.java
public class CommaOperator {
public static void main(String[] args) {
for(int i = 1, j = i + 10; i < 5;
i++, j = i * 2) {
System.out.println("i= " + i + " j= " + j);
}
}
} ///:~
>>> 在 ? : 中也可以用 "," 吗? k=(k>0)?(p++, k=0):k+1;
解决方案 »
- 如何求两个数组的交集?
- 类调用的简单问题+Arraylist (我是菜鸟,大哥们们帮帮忙)在线等!!!得到答案马上结贴。分少,见谅!
- Java有哪些应用领域?
- 受不了了!超级菜鸟问题!最后50分了!!绝对超级菜鸟问题~
- 可调整尺寸border定制问题
- 关于JAR自动运行的问题
- 急急急。为什么不能运行窗口程序?
- 为什么没人来(laocai8),真失望,雷锋哪去了?????????
- 奇怪,究竟有多少人用JBuilder的project功能,我看很多人都只是简单用用JBuilder?
- Java中有没有类似于全局变量这样的东西?具体问题见内容:
- 恭贺诸位新年快乐
- java调用外部程序javac.exe时,文件路径如果有空格,怎么避免?
记得Java中的逗号运算符只能用于"for"循环