[code=Java]
[class A
{int x,y,z;
A(int a,int b)
 {
   x=a;
   y=b;
 }
int getMax()
{
if(x<y)
{
int e=y;
    y=x;
    x=e;
}
do{
z=x%y;
x=y;
y=z;
}while(z!=0);
return x;

}
class B
{
}
class C
{
public static void main(String [] args)
{
A s=new A(10,5);
s.getMax();
System.out.println(s.getMax());
}
}/code]

解决方案 »

  1.   

    A的getMax()会改变x,y的值,你第一次调用这个方法后,s.x变成5,s.y变成0.
    你在System.out.println(s.getMax())这一句第二次调用时候,会出现除0的异常
      

  2.   


    class A {
    int x,y,z=10000; 
        A(int a,int b) { 
          x=a; 
          y=b; 
        } 
       int getMax() { 
         if(x <y) { 
          int e=y; 
          y=x; 
          x=e; 
         } 
        
        while(z!=0){
          z = x % y;
          x = y;
          y = z;
         } 
      return x; 
      } 

    class B{ } 
    class C { 
          public static void main(String [] args) { 
             A s=new A(300,99); 
             s.getMax(); 
             System.out.println(s.getMax()); 
          } 
    }
      

  3.   

    这样做能够正常输出class A {
    int x,y,z=10000; 
        A(int a,int b) { 
          x=a; 
          y=b; 
        } 
       int getMax() { 
         if(x <y) { 
          int e=y; 
          y=x; 
          x=e; 
         } 
        
        while(z!=0){
          z = x % y;
          x = y;
          y = z;
         } 
      return x; 
      } 

    class B{ } 
    class C { 
          public static void main(String [] args) { 
             A s=new A(300,99); 
             s.getMax(); 
             System.out.println(s.getMax()); 
          } 
    }
      

  4.   

    LZ用的展转相除
    还有一种 public static void main(String str[]){ 
    getMaxCom(-20);


    public static int getMaxCom(int c){
    int b = Math.abs(c);
    if(b<=2){

    System.out.println("没有最大公约数");
    }
    for(int i =2;i<b;i++){

    if(0==b%i){
    System.out.println("最大公约数=="+b/i);
    return b/i;

    }
    }
    return 0;
    }