永不退缩 永不退缩

I,can do

目录
HeadFistJava-EchoTestDrive(泳池迷宫)
/  

HeadFistJava-EchoTestDrive(泳池迷宫)

你的任务是要从游泳池中挑出程序片段并将它填入右边的空格中。
同一个片段不能使用两次,且游泳池中有些多余的片段。填完空格的程序必须要能够编译与执行并产生出下面的输出

hellooooo

public class EchoTestDrive {
  public static void main(String [] args) {
	Echo e1 = new Echo();   
	_________________________   
	int x = 0;   
	while ( ___________ ) {     
	  e1.hello();     
	  __________________________     
	  if ( ____________ ) {
		e2.count = e2.count + 1;     
	  }     
	  if ( ____________ ) {       
		e2.count = e2.count + e1.count;     
	  }     
	  x = x + 1;   
	}   
	System.out.println(e2.count);   
  } 
}

class ____________ {
  int _________ = 0;
  void ___________ {
	System.out.println(“helloooo... “);
  }   
}

提供的“池中物”有:
备选

开始的思路有:

  1. e2在while里外均有调用,所以Echo e1 = new Echo();下面应该是声明了e2
  2. hellooooo输出了四遍,且“池中物”没有e2.hello(),说明:Class是Echo,其中的方法是hello,while的条件是x<4。
  3. Echo里面的属性是count

e2.count最终结果为10:

class EchoTestDrive {

  public static void main(String[] args) {

	Echo e1 = new Echo();

	Echo e2 = new Echo();

	int x = 0;

	while(x<4){ // x == 0 1 2 3 4

	  e1.hello();

	  e1.count = e1.count+1; // e1.count == 1 2 3 4 5

	  if( x == 3 ){

		e2.count = e2.count+1;  // e2.count == 1 3 8 4

	  }

	  if( x > 0 ){

		e2.count = e2.count+e1.count; // e2.count == 2 5 6 8

	  }

	  x = x + 1;

	}

	System.out.println(e2.count);

  }

}

class Echo {

  int count = 0;

  void hello() {

	System.out.println("helloooooooo... ");

  }

}

如果最后一行的输出不是10而是24,空格又应该如何填呢?

class EchoTestDrive {

  public static void main(String[] args) {

	Echo e1 = new Echo();

	Echo e2 = e1;

	......
	.....
	..

原因:
将e1的值(对象的地址信息)赋给e2,则e2和e1指向相同的对象。对象的实际属性发生变化,则通过所有的引用去调用对象时,操作的还是同一个。