思路很简单,直接上代码
package go.derek; public class Judge { //递归方法 public boolean judge1(double n){ n/=2; if(n==2){ return true; } //如果始终不等于2,则肯定有以下两种情况 if(1<n&&n<2||2<n&&n<3){ return false; } return judge1(n); } //循环方法 public boolean judge2(double n){ while(n!=2){ n/=2; if(1<n&&n<2||2<n&&n<3){ return false; } } return true; } public static void main(String[] args){ boolean b1=new Judge().judge1(128); boolean b2=new Judge().judge2(168); System.out.println(b1); System.out.println(b2); } }
输出
true
false