java求迴圈次數
阿新 • • 發佈:2018-11-09
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)); } }