JAVA 使用super关键字调用父类中的方法

图片说明
图片说明
本来返回值没有问题,但到下一行m就变成了0
图片说明
源码

 package projcet_zuoye;
import java.util.Scanner;

class A1{
    public int gcd(int a,int b) {
        return b==0?a:gcd(b,a%b);
    }
    public int f(int a,int b) {
        if(a>b)
            return gcd(a,b);
        else
            return gcd(b,a);
    }
}

class B1 extends A1{
    public int f(int a,int b) {
         int m = super.f(a,b);
        if(0 == m)
            return a*b;
        else
            return (a*b)/m;
    }
}

public class Example_9 {

    public static void main(String[] args) {
        Scanner reader = new Scanner(System.in);
        int a,b;
        System.out.println("请输入运算的数a,b");
        a = reader.nextInt();
        b = reader.nextInt();
        A1 a1 = new A1();
        B1 b1 = new B1();
        System.out.println(a+","+b+"的最大公约数是:"+a1.f(a, b));
        System.out.println(a+","+b+"的最小公倍数是:"+b1.f(a, b));
    }

}

我代码有点问题已经解决

class B1 extends A1{
public int f(int a,int b) {
int m = super.f(a,b);
if(0 == m)
return a*b;
else
return (a*b)/m;
}
}

看不出来什么时候m会是0

解决了就好。采纳了姐姐的回答,下次你要下载5分以内的资源,姐姐帮你下。

package projcet_zuoye;
import java.util.Scanner;

class A1{
public int gcd(int a,int b) {
return b==0?a:gcd(b,a%b);
}
public int f(int a,int b) {
if(a>b)
return gcd(a,b);
else
return gcd(b,a);
}
}

class B1 extends A1{
public int f(int a,int b) {
int m = super.f(a,b);
if(0 == m)
return a*b;
else
return (a*b)/m;
}
}

public class Example_9 {

public static void main(String[] args) {
    Scanner reader = new Scanner(System.in);
    int a,b;
    System.out.println("请输入运算的数a,b");
    a = reader.nextInt();
    b = reader.nextInt();
    A1 a1 = new A1();
    B1 b1 = new B1();
    System.out.println(a+","+b+"的最大公约数是:"+a1.f(a, b));
    System.out.println(a+","+b+"的最小公倍数是:"+b1.f(a, b));
}

}