System.out.printf( "%5d", method( 12 ) );
   System.out.println();
}

public static int method( int 12 ){
   if ( No == 1){
      return 1;
   }

   int bob = 2 * method ( 12 - 1 );

   return bob;
}


我的程序可以打印二进制序列;但只有最后一个学期。
例如)N = 12; 2048
但是我希望我的程序打印1 2 4 8 16 32 64 128 256 512 10242048。我迷路了

最佳答案

将print语句包括在return语句之前的count方法中

像这样的东西:

   public static int count( int n ){
   if ( n == 1)
   {
      System.out.printf( "%15d", 1);
      return 1;
   }

   int nTerms = 2 * count ( n - 1 );
   System.out.printf( "%15d", nTerms );

   return nTerms;
}

07-24 18:58
查看更多