大家好!我是JAVA的初学者,现在有个问题请大家帮我解答,代码如下:
public class Mix4 {
   int counter=0;
   public static void main(String[]args) {
     int count=0;
     Mix4 [] m4a=new Mix4[20];
     int x=0;
     while ( x<19) {
       m4a[x]=new Mix4();
       m4a[x].counter=m4a[x].counter + 1;
       count=count+1;
       count=count+m4a[x].maybeNew(x);       x=x+1;
     }
      System.out.println(count + " " + m4a[1].counter);
   }
  public int maybeNew(int index) {     
if ( index<1 ) {
    Mix4 m4=new Mix4();
     m4.counter=m4.counter+1;
     return 1;
    }
    return 0;
  } 
}
它的输出是: 20  1。请问最好後的return 1和return 0分别返回到那里?
谢谢!

解决方案 »

  1.   

    count=count+m4a[x].maybeNew(x);
    ================================
    返回到这里啊,第一次循环的时候,conunt = 0 + m4a[0].maybeNew(x);==conunt = 0 + 1 = 1
    以后每次的maybeNew(x);都会返回0,conunt就+1,又由于conunt再上面还+1(count=count+1;),所以19次循环过后,count = 20
    而m4a[x].counter一直会是1
      

  2.   

    public int maybeNew(int index) {     
    这个方法结束返回值是int 也就是你return 的1 或0。也就是返回到调用这个这个方法的地方。
      

  3.   

    为什么而m4a[x].counter一直会是1?