如何用JAVA實現找到100~999的完全平方數(轉載自:邵發)
阿新 • • 發佈:2018-12-16
如何用JAVA實現找到100~999的完全平方數(轉載自:afanihao.cn)
//主程式 package fuckthismy; public class helloworld { public static void main(String[] args) { Mymath ex=new Mymath(); for(int i=100;i<999;i++)//檢查從100到999的完全平方數 { boolean yes=ex.check(i); if(yes) { System.out.println("完全平方數:"+i); } } } } //建立一個類 public boolean check(int n) { if (n <= 0) { return false;// 排除負數 } int half = n / 2; for (int i = 1; i <= half; i++)// 給出一個數n,// 判斷這個n是不是完全平方數 { if (i * i == n) { return true;// 是完全平方數。OK } } return false; }