可能重复:
java operator ++问题
这是代码.我知道c ++和++ c之间的区别.
public class sample {
public static void main(String[] b){
int count = 0,a=0;
for (int i = 0; i < 3; i++){
count=count++;
System.out.println(count);
}
Run Code Online (Sandbox Code Playgroud)
但我期待的是
count=count;count=count+1;//output has to be 1 2 3
Run Code Online (Sandbox Code Playgroud)
但输出为0 0 0.
Mar*_*in. 15
你的预测是错误的.
count ++将count递增1并返回旧值(0).你的情况如何?然后,将旧值(0)分配给count变量.为了使其更易理解,请查看此代码
count = count; // is the same as count = count++;
Run Code Online (Sandbox Code Playgroud)
不要使用count = count++;,只需使用count++
在Java中,保证此代码保持变量具有相同的值.
就像是:
int temp;
temp = count;
count = count +1;
count = temp;
Run Code Online (Sandbox Code Playgroud)
为了得到你想要的东西,写道:
count++; //or
count += 1; //or
count = count +1;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
17642 次 |
| 最近记录: |