//最大公约数和最大公约数问题
//class A{
public int f(int a,int b){
int num1=0;int num2=0;int temp=0;
if(num1>num2) /*找出两个数中的较大值*/
{
temp=num1; num1=num2; num2=temp; /*交换两个整数*/
}
a=num1; b=num2;
while(b!=0) /*采用辗转相除法求最大公约数*/
{
temp=a%b;
a=b;
b=temp;
}
return a;
}
}
class B extends A{
public int f(int a,int b){
int c;
c=super.f();
a=super.a;
b=super.b;
return a*b/c;
}
}
public class C{
public static void main(String args[]){
A a= new A();
a.f(12,18);
System.out.println("a"+a+"和b"+"b"+b+"的最大公约数是"+a.f());
B b= new B();
b.f(12,18);
System.out.println("a"+a+"和b"+"b"+b+"的最小公倍数是"+b.f());
}
}
//class A{
public int f(int a,int b){
int num1=0;int num2=0;int temp=0;
if(num1>num2) /*找出两个数中的较大值*/
{
temp=num1; num1=num2; num2=temp; /*交换两个整数*/
}
a=num1; b=num2;
while(b!=0) /*采用辗转相除法求最大公约数*/
{
temp=a%b;
a=b;
b=temp;
}
return a;
}
}
class B extends A{
public int f(int a,int b){
int c;
c=super.f();
a=super.a;
b=super.b;
return a*b/c;
}
}
public class C{
public static void main(String args[]){
A a= new A();
a.f(12,18);
System.out.println("a"+a+"和b"+"b"+b+"的最大公约数是"+a.f());
B b= new B();
b.f(12,18);
System.out.println("a"+a+"和b"+"b"+b+"的最小公倍数是"+b.f());
}
}
C.java:28: 无法将 A 中的 f(int,int) 应用于 ()
c=super.f();
^
C.java:29: 找不到符号
符号: 变量 a
位置: 类 A
a=super.a;
^
C.java:30: 找不到符号
符号: 变量 b
位置: 类 A
b=super.b;
^
C.java:41: 找不到符号
符号: 变量 b
位置: 类 C
System.out.println("a"+a+"和b"+"b"+b+"的最大公约数是"+a.f());
^
C.java:41: 无法将 A 中的 f(int,int) 应用于 ()
System.out.println("a"+a+"和b"+"b"+b+"的最大公约数是"+a.f());
^
C.java:45: 找不到符号
符号: 方法 f()
位置: 类 B
System.out.println("a"+a+"和b"+"b"+b+"的最小公倍数是"+b.f());
^
6 错误
这个问题其实不是很适合使用继承。
class A { public int f(int a, int b) { int temp = 0;
if (b > a) /*找出两个数中的较大值*/ {
temp = b;
b = a;
a = temp; /*交换两个整数*/
}
while (b != 0) /*采用辗转相除法求最大公约数*/ {
temp = a % b;
a = b;
b = temp;
}
return a;
}
}class B extends A { public int f(int a, int b) { int c = super.f(a, b);
return a * b / c;
}
}class C { public static void main(String args[]) { A objA = new A();
int a = 12;
int b = 18;
int gcd = objA.f(a, b);
System.out.println("a=" + a + "和b=" + b + "的最大公约数是" + gcd); B objB = new B();
int times = objB.f(a, b);
System.out.println("a=" + a + "和b=" + b + "的最小公倍数是" + times); }
}
class A {
public int f(int a, int b) {
int num1 = 0;
int num2 = 0;
int temp = 0;
if (a > b) /* 找出两个数中的较大值 */
{
temp = a; /*你在交换都为0值的num1和num2 有用吗*/
a = b;
b = temp; /* 交换两个整数 */
} while (b != 0) /* 采用辗转相除法求最大公约数 */
{
temp = a % b;
a = b;
b = temp;
}
return a;
}
}class B extends A {
public int f(int a, int b) {
int c;
c = super.f(a,b); /* 好好看看super是什么意思 */
return a * b / c;
}
}public class C {
public static void main(String args[]) { A a = new A();
int number1 = 12;
int number2 = 18;
int yueshu = a.f(number1, number2);
System.out.println("a" + number1 + "和b" + number2 + "的最大公约数是" + yueshu); /* 你变量b是哪里来的 当然会报错*/ B d = new B();
int gongbei = d.f(number1, number2);
System.out.println("a" + number1 + "和b" + "b" + number1 + "的最小公倍数是" + gongbei); }}不要只是copy代码这样没有提高的
class A { public int f(int a, int b) {
int temp = 0;
if (a > b) /* 找出两个数中的较大值 */
{
temp = a;
a = b;
b = temp; /* 交换两个整数 */
} while (b != 0) /* 采用辗转相除法求最大公约数 */
{
temp = a % b;
a = b;
b = temp;
}
return a;
}
}
class B extends A {
public int f(int a, int b) {
int c;
c = super.f(a, b);
return a * b / c; }
}
public class C {
public static void main(String args[]) {
A a = new A();
System.out.println("a" + "和b" + "的最大公约数是" + a.f(12, 18));
B b = new B();
System.out.println("a" + "和b" + "的最小公倍数是" + b.f(12, 18)); }}
楼主错误太多了,int num1=0;int num2=0;这句是不必要的。
a=super.a; b=super.b; 子类会自动继承父类的a,b,这句也不要写。
System.out.println("a"+a+"和b"+"b"+b+"的最大公约数是"+a.f()); System.out.println("a"+a+"和b"+"b"+b+"的最小公倍数是"+b.f()); 输出语句也有错误,直接写成System.out.println("a和b的最大公约数是" + a.f(12, 18));就行了。
//
class AA{
public int f(int a,int b){
//int num1=0;int num2=0;
int temp=0;
if(b>a) /*找出两个数中的较大值*/
{
temp=b; b=a; a=temp; /*交换两个整数*/
}
while(b!=0) /*采用辗转相除法求最大公约数*/
{
temp=a%b;
a=b;
b=temp;
}
return a;
}
}class BB extends AA{
public int f(int a,int b){
int c=super.f(a,b);
//a=super.a;
//b=super.b;
return a*b/c;
}
}public class CC{
public static void main(String args[]){
AA objAA= new AA();
int a=12;
int b=18;
//objAA.f(a,b);
System.out.println("a="+a+"和b"+"b="+b+"的最大公约数是"+objAA.f(a,b));
BB objBB= new BB();
//objBB.f(a,b);
System.out.println("a="+a+"和b"+"b="+b+"的最小公倍数是"+objBB.f(a,b));
}
}/*C:\java>javac CC.javaC:\java>java CC
a=12和bb=18的最大公约数是6
a=12和bb=18的最小公倍数是36*//*
CC.java:24: 无法将 AA 中的 f(int,int) 应用于 ()
c=super.f();
^
CC.java:25: 找不到符号
符号: 变量 a
位置: 类 AA
a=super.a;
^
CC.java:26: 找不到符号
符号: 变量 b
位置: 类 AA
b=super.b;
^
3 错误*//*
CC.java:24: 无法将 AA 中的 f(int,int) 应用于 ()
c=super.f();
^
1 错误
*//*
a12和bb18的最大公约数是0
Exception in thread "main" java.lang.ArithmeticException: / by zero
at BB.f(CC.java:27)
at CC.main(CC.java:39)
*/