class Echo{
int count = 0;
void hello(){
System.out.println("hellooo");
}
}public class EchoTestDrive{
public static void main(String[] args){
Echo e1 = new Echo();
Echo e2 = new Echo();
int x = 0;
while(x<4){
e1.hello();
e1.count=e2.count + 1;
if(x==3){
e2.count=e2.count+1;
}
if(x>0){
e2.count=e2.count+e1.count;
}
x=x+1;
}
System.out.println(e2.count);
}
}
hellooo
hellooo
10对于结果为10,我不是很清楚循环的过程,请大家帮忙分析下,如果为结果为24,又该怎样循环,对于这样的题有什么好的分析方法吗,请大家多指教,不胜感激!!!
对着看一下就明白了
class Echo
{
int count = 0;
void hello()
{
System.out.println("hellooo");
}
}public class EchoTestDrive
{
public static void main(String[] args)
{
Echo e1 = new Echo();
Echo e2 = new Echo();
int x = 0;
while(x<4)
{
e1.hello();
e1.count=e2.count + 1;
if(x==3)
{
e2.count=e2.count+1;
}
if(x>0)
{
e2.count=e2.count+e1.count;
}
x=x+1;
System.out.println(e1.count + " " + e2.count);
}
System.out.println(e2.count);
}
}
e1.cout=1
e2.cout=0
x=1
e1.cout=1
e2.cout=1
x=2
e1.cout=2
e2.cout=3
x=3
e1.cout=4
e2.cout=8我运行出来是8
while(x<4){///////循环4次
e1.hello();
e1.count=e2.count + 1;
if(x==3){///////////////最后循环一次才自加
e2.count=e2.count+1;
}
if(x>0){
e2.count=e2.count+e1.count;///////x>0时相当于每次e2.count加上e1.count的值
}
x=x+1;
}
x=0
e1.count=0+1=1
x=1
e1.count=0+1=1 e2.count=1
x=2
e1.count=2 e2.count=3
x=3
e1.count=4 x=3,e2.count=4 e2.count=8