zjingye 我去面试遇到了两道关于i++的 题在此总结一下。
题目一:
int i=0;
while(i++<10){
System.out.printl(i+",");
}
答案是:1,2,3,4,5,6,7,8,9,10
自己写了一个分析程序
int i=0;
if (i++ == 1) {
System.out.print("true");
System.out.print(i);
} else {
System.out.print("false");
System.out.print(i);
}
输出结果是 false 1
分析:
i++ 是在i和10比对之后才加1的即
int i=0;
while(i<10){
i = i+1; //注意i自增是在这里
System.out.printl(i+",");
}
题目二:
public static void main(String[] args) {
// TODO Auto-generated method stub
int i = 0;
for (f('A');f('B')&&i<2;f('C')) {
i++;
}
}
public static boolean f(char c){
System.out.print(c+" ");
return true;
}
输出结果:A B C B C B
分析:
for(语句1;语句2;语句3){}的执行流程
1:执行语句1 赋值语句,
2: 执行语句2 比较语句 满足条件执行方法体{} 不满足条件退出for循环,
3:{}方法体执行
4: 执行语句3
5:跳到步骤2
题目一:
int i=0;
while(i++<10){
System.out.printl(i+",");
}
答案是:1,2,3,4,5,6,7,8,9,10
自己写了一个分析程序
int i=0;
if (i++ == 1) {
System.out.print("true");
System.out.print(i);
} else {
System.out.print("false");
System.out.print(i);
}
输出结果是 false 1
分析:
i++ 是在i和10比对之后才加1的即
int i=0;
while(i<10){
i = i+1; //注意i自增是在这里
System.out.printl(i+",");
}
题目二:
public static void main(String[] args) {
// TODO Auto-generated method stub
int i = 0;
for (f('A');f('B')&&i<2;f('C')) {
i++;
}
}
public static boolean f(char c){
System.out.print(c+" ");
return true;
}
输出结果:A B C B C B
分析:
for(语句1;语句2;语句3){}的执行流程
1:执行语句1 赋值语句,
2: 执行语句2 比较语句 满足条件执行方法体{} 不满足条件退出for循环,
3:{}方法体执行
4: 执行语句3
5:跳到步骤2