首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

java while循环有关问题

2012-03-09 
java while循环问题public class Shufflel {public static void main(String[] args) {int x 3while (

java while循环问题
public class Shufflel {
  public static void main(String[] args) {
  int x = 3;
  while ( x > 0) {
  if(x>2) {
  System.out.print("a");
  }
  x = x - 1;
  System.out.print("-");
  if(x==2) {
  System.out.print("b c");
  }
  if(x==1) {
  System.out.print("d");
  x = x - 1;
  }
  }
  }
}
 本人最近才开始学JAVA编程,这个例子我看不懂,谁能帮我分析一下运算过程。特别是C - D 是怎么算出来的。谢谢。

[解决办法]
x大于零进入while循环,while循环中的代码也是从上到下执行,先输出a;再输出-;此时x=2,则下面的if语句成立,输出bc;此为第一轮循环,输出a-bc。
第二轮循环输出a-d;此时x为零,跳出循环!!!!!

热点排行