class Gys//Gys代表公约数
{
    static int x,y,temp;
    void bi(Gys a)//比较a,b.使b>a
    {
     if(a.x>a.y)
     {
         temp=a.x;
         a.x=a.y;
         a.y=temp;
     }
    }int shuang(int a,int b)//求最大公约数
    {
     int n,o=0;
 if(b%a==0&&(a+o)%a==0)
{
      a=a;
    }
else
{
  o++;
  --a;
  shuang(a,b);
}
return a;
     }
public static void main(String[]args)
    {
     int p;
     Gys shu=new Gys();
     shu.x=Integer.parseInt(args[0]);
     shu.y=Integer.parseInt(args[1]);
     shu.bi(shu);
     p=shu.shuang(x,y);
     System.out.println("a与b的最大公约数为:"+p);
    }
}

解决方案 »

  1.   

    我改了一下,可是还是不行,不重知道这个返回值怎么写
    class Gys//Gys代表公约数
    {
        static int x,y,temp;
        void bi(Gys a)//比较a,b.使b>a
        {
         if(a.x>a.y)
         {
             temp=a.x;
             a.x=a.y;
             a.y=temp;
         }
        }int shuang(int a,int b,int c)//求最大公约数
        {
         int n,o=0;
     if(b%a==0&&c%a==0)
    {
          a=a;
      return a;
        }
    else
    {
      o++;
      --a;
      shuang(a,b,c);
      return a;
    }

         }
    public static void main(String[]args)
        {
         int p;
         Gys shu=new Gys();
         shu.x=Integer.parseInt(args[0]);
         shu.y=Integer.parseInt(args[1]);
         shu.bi(shu);
         p=shu.shuang(x,y,shu.x);
     System.out.println("a与b的最大公约数为:"+p);
        }
    }
      

  2.   

    int   shuang(int   a,int   b,int   c)//求最大公约数 你把shu.x传给c
            { 
              int   n,o=0; 
      if(b%a==0&&c%a==0) 

                a=a; 
        return   a; 
            } 
    else 

        o++; 
        --a; a--代表将X自减1,你的c也代表x,那么它也会跟着减小,他么都是类的成员
        shuang(a,b,c); 
        return   a; 

      

  3.   

    这样就可以了
    int       shuang(int       a,int       b,int       c)
    {   
    System.out.println("c:"+c+"a:"+a+"b:"+b+"c:"+c);
        if(b%c==0&&a%c==0)
    {
            return c;   
        }   
    else   
    {    
            c--;
            c=shuang(a,b,c);
            return c;  
    }
    }第一次传时把a的值给了c
      

  4.   


    public class TestGYS {
    public static void main(String[] args){
    TestGYS testGYS = new TestGYS();
    int x = 15;
    int y = 6;
    int gys = testGYS.getGYS(x,y);
    System.out.println(x + "和" + y + "的公约数:" + gys);

    }
    /**
     * 下面这个函数就是楼主想要的函数了,非常简单,不要想复杂了
     * @param x
     * @param y
     * @return
     */
       public int getGYS(int x,int y){
       if( x%y == 0){
       return y;
       }
       else{
       return getGYS(y,x%y);
       }
       }
    }
      

  5.   

    public class   Gys//Gys代表公约数