光辉岁月 发表于 2006-6-1 22:46:19

请JAVA高手指教

JAVA高手帮我看一下这个程序有什么问题啊
这是:   
验证“哥德巴赫猜想”:任何大于6的偶数均可以表示为两个素数之和。
class yanss
{
   boolean yan(int x)
   {
           int s=0;
           for (int i=1;i<=x ;i++ )
           {
                  if ((x%i)==0) s++;
           }
           if (s==2) return true;
   }
}
class test
{
        public static void main(String[] args)
        {
                int n=0;
                for (int a=8;a<=100 ;a +=2 )
                {
                        yanss ob=new yanss();
                                if (ob.yan(a)) n++;
                }
                if (n==47)System.out.println("猜想正确啊!!");
        }
}
最后编绎出现这个错误:missing return statement

xiaohui20031984 发表于 2006-6-3 15:33:06

注意在if的另一种情况的返回值啊

class yanss
{
   boolean yan(int x)
   {
         int s=0;
         for (int i=1;i<=x ;i++ )
         {
                  if ((x%i)==0) s++;
         }
         if (s==2) return true;
         
         return false;
   }
}
class test
{
      public static void main(String[] args)
      {
                int n=0;
                for (int a=8;a<=100 ;a +=2 )
                {
                        yanss ob=new yanss();
                              if (ob.yan(a)) n++;
                }
                if (n==47)System.out.println("猜想正确啊!!");
      }
}

光辉岁月 发表于 2006-6-3 18:26:09

多谢了但还一个问题啊

这个程序好像还是错的啊
高手还能不能帮一下小弟啊
语法现在没有错了
页: [1]
查看完整版本: 请JAVA高手指教