下列程序的执行结果是

来源:百度知道 编辑:UC知道 时间:2024/05/21 17:06:03
public class ex15{
public static void main(String[] args) }
int j=2,i=8;
while(j<--i){
for(int k=0;k<4;k++ )
j++;
}
System.out.print(j);
}
}

A)10 B)6 C)4 D)2

解释一下过程 要详细

答案是 B 6
public class ex15{
public static void main(String[] args) { // 这里应该是“{”吧?
int j=2,i=8; //初始化
while(j<--i){ // 第一次循环时,j是2 ,--i 是7,循环条件成立
// 第二次循环时,j是6 ,--i 是6,循环条件不成立
for(int k=0;k<4;k++ )
j++; // 第一次循环时,j自加了4次 变成6
}
System.out.print(j);
}
}
如果我的回答能对你有点用,我会感到非常荣幸。

public class ex15{
public static void main(String[] args) }
int j=2,i=8; //初始化
while(j<--i){ //j=2,小于--i(为7)成立;第二次j=6,小于--i(为6)不成立while循环结束
for(int k=0;k<4;k++ )//for循环不做多余解释
j++;//当第一轮while循环完毕时候j=6
}
System.out.print(j); //输出6;
}
}
所以选择b