永发信息网

用java写一个求最大公约数和最小公倍数的问题。

答案:3  悬赏:70  手机版
解决时间 2021-03-10 14:48
  • 提问者网友:鼻尖触碰
  • 2021-03-10 03:55
import java.util.*;
///求最大公约数 父类
class Maxcommon {
public int a , b , i , commyue ;
public Maxcommon(int a , int b){
this.a = a ;
this.b = b ;
i = 1 ;
commyue = 0;
}
public void F1(){ //////F1代表求最大公约是的方法
int c = a ;
if (c < b)
c = b ;
while( i <= c){
if ( a % i == 0 && b % i == 0)
commyue = i ;
i++ ;
}
System.out.println("两个数的最大公约数是=" + commyue );
}
}
class Mincommon extends Maxcommon{
private int commbe ;
public int a , b ;
public Mincommon( int a , int b){
super( a , b);
}
public void F2(){
this.commbe = this.a * this.b / super.commyue ;
System.out.println("两个数的最小公倍数是:" + commbe);
}
}

public class Common {
public static void main(String []args){
Scanner sc = new Scanner(System.in);
System.out.println("请输入要比较的两个数:");
Maxcommon mac = new Maxcommon(sc.nextInt(),sc.nextInt()) ;
Mincommon mic = new Mincommon(mac.a , mac.b) ;
mac.F1();
mic.F2();
}
}

语法没有问题,最小公倍数出不来。哪位大神帮忙一下。
最佳答案
  • 五星知识达人网友:底特律间谍
  • 2021-03-10 04:59
public void F2(){
this.commbe = this.a * this.b / super.commyue ;
System.out.println("两个数的最小公倍数是:" + commbe);
}
你这里面的this.commbe = this.a * this.b / super.commyue ;你知道super.commyue的值是几吗?因为你继承了最大公约数的类,而那个类的构造函数里将commyue赋值为0,所以你这条除法算式的除数为零,是错的。我想不懂你的最小公约数的类干嘛要继承最大公约数的类。
全部回答
  • 1楼网友:平生事
  • 2021-03-10 06:26
import java.util.*; public class lianxi06 { public static void main(string[] args) { int a ,b,m; scanner s = new scanner(system.in); system.out.print( "键入一个整数: "); a = s.nextint(); system.out.print( "再键入一个整数: "); b = s.nextint(); deff cd = new deff(); m = cd.deff(a,b); int n = a * b / m; system.out.println("最大公约数: " + m); system.out.println("最小公倍数: " + n); } } class deff{ public int deff(int x, int y) { int t; if(x < y) { t = x; x = y; y = t; } while(y != 0) { if(x == y) return x; else { int k = x % y; x = y; y = k; } } return x; } }
  • 2楼网友:不想翻身的咸鱼
  • 2021-03-10 06:21
[java] view plaincopy import java.util.*; public class MaxCommonDivisorAndMinCommonMultiple { public static void main(String[] args) { Scanner scan = new Scanner(System.in);// 接收控制台输入的信息 System.out.print("请输入第一个整数:"); int num1 = scan.nextInt(); // 取出控制台输入的信息 System.out.print("请输入第二个整数:"); int num2 = scan.nextInt(); // 取出控制台输入的信息 System.out.println(maxCommonDivisor(num1, num2));// 调用maxCommonDivisor()方法 System.out.println(minCommonMultiple(num1, num2));// 调用minCommonMultiple()方法 } // 递归法求最大公约数 public static int maxCommonDivisor(int m, int n) { if (m < n) {// 保证m>n,若m<n,则进行数据交换 int temp = m; m = n; n = temp; } if (m % n == 0) {// 若余数为0,返回最大公约数 return n; } else { // 否则,进行递归,把n赋给m,把余数赋给n return maxCommonDivisor(n, m % n); } } // 循环法求最大公约数 public static int maxCommonDivisor2(int m, int n) { if (m < n) {// 保证m>n,若m<n,则进行数据交换 int temp = m; m = n; n = temp; } while (m % n != 0) {// 在余数不能为0时,进行循环 int temp = m % n; m = n; n = temp; } return n;// 返回最大公约数 } // 求最小公倍数 public static int minCommonMultiple(int m, int n) { return m * n / maxCommonDivisor(m, n); } }
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯