1. 程式人生 > >java求迴圈次數

java求迴圈次數

public class circulation {

        public int  times( int n){

            int i = 1;
            int count = 0;
            while (i <= n) {
                i = i * 2;
                count++;
            }
               return count;

        }
        
        /* 可以不用return返回
         public void times(int n)
         {

            int i = 1;
            int count = 0;
            while (i <= n) {
                i = i * 2;
                count++;
                }
                System.out.println(count);
         }
         */

    }



//測試



public class circulationtest {
    public static void main(String[] args)
    {
        circulation c=new circulation();
        
          //c.times(4); 如果不用return時
          
        System.out.println(c.times(4));

    }
}