日期:2014-05-20  浏览次数:20594 次

求高手帮我解释下这段代码
class   blocktest{
public   static   void   main   (String   args[])
{int   a,b;
    b=20;
    for(a=0;a <10;a++){
    System.out.println( "this   is   a: "+a);
    System.out.println( "this   is   b: "+b);
    b=b-2;
    }
}
}
 

运行结果:
this   is   a:0
this   is   b:20
this   is   a:1
this   is   b:18
this   is   a:2
this   is   b:16
this   is   a:3
this   is   b:14
this   is   a:4
this   is   b:12
this   is   a:5
this   is   b:10
this   is   a:6
this   is   b:8
this   is   a:7
this   is   b:6
this   is   a:8
this   is   b:4
this   is   a:9
this   is   b:2
Press   any   key   to   continue...
为什么b   回逐渐   减小????

------解决方案--------------------
因为 b=b-2啊
a每自增一次 b就会减少2
------解决方案--------------------
每执行一次循环,就先输出a和b的值,然后让b减2,然后再把这个值赋给b,既b=b-2,这时b的值和原来比就减少了2,循环一直执行到条件不满足为止。所以b的值也就一直在减少。
------解决方案--------------------
应为 b=b-2 啊 !~~
------解决方案--------------------
..................
------解决方案--------------------
b=b-2;

晕啦
------解决方案--------------------
:)
------解决方案--------------------
有点意思!!!
------解决方案--------------------
LZ去看谭的C吧 再把一本配套习题全做完!
以后考C 你可以闭上眼睛了!!!!!(国内考试均有效)
------解决方案--------------------
应为b=b-2;在for循环体内,所以每循环一次都要执行一次b-2,再赋值给b,直到不满足条件a <10
------解决方案--------------------
class blocktest{
public static void main (String args[])
{int a,b;
b=20;
for(a=0;a <10;a++){
System.out.println( "this is a: "+a);
System.out.println( "this is b: "+b);
b=b-2;//因为这个
}
}
}
------解决方案--------------------
关键在b=b-2!!!
------解决方案--------------------
b=b-2
------解决方案--------------------
class blocktest{
public static void main (String args[])
{int a,b;
b=20;
for(a=0;a <10;a++){
System.out.println( "this is a: "+a);
System.out.println( "this is b: "+b);
b=b-2; //它每次循环都执行一次 -2
}
}
}
------解决方案--------------------
佛山无影脚~~
------解决方案--------------------
建议好好补补基础

------解决方案--------------------
晕一会儿~~
------解决方案--------------------
加油!
------解决方案--------------------