java 求最大公约数,最小公倍数

利用辗转相除法求两个整数的最大公约数和最小公倍数。注意,这里只写一个方法求最大公约数,最小公倍数放到main方法中完成。

import java.util.Scanner;
public class Chpt7_4 {
public static int fun1(int a1,int b1)
{ int c;
c=a1%b1;
while (c>0)
{a1=b1;
b1=c;
c=a1%b1;
return (b1);
}
}
public static void main(String[] args) {
int a,b,temp;
int m;
Scanner in=new Scanner(System.in);
a=in.nextInt();
b=in.nextInt();
if(a<b)
{temp=a;a=b;b=temp;
}
m=fun1(a,b);
System.out.println("最大公约数为:"+m);
System.out.println("最小公倍数为:"+(a*b)/m);
}

我改不对,求知道!
import java.util.Scanner;
public class C{
public static int fun1(int a1,int b1){
int c;
c=a1%b1;
while (c>0)
{a1=b1;
b1=c;
c=a1%b1;
//和清旅return b1;
}
return b1; //把return写在这里;
}
public static void main(String[] args) {
int a,b,temp;
int m;
Scanner in=new Scanner(System.in);
a=in.nextInt();
b=in.nextInt();
if(a<b)
{temp=a;a=b;b=temp;
}
m=fun1(a,b);
System.out.println("最大公约唤凳数为:"正段+m);
System.out.println("最小公倍数为:"+(a*b)/m);
}
}
import java.util.Scanner;

public class Chpt7_4 {
public static int fun1(int a1, int b1) {
int c;
c = a1 % b1;
while (c > 0) {
a1 = b1;
b1 = c;
c = a1 % b1;
}
return (b1);
}

public static void main(String[] args) {
int a, b, temp;
int m;
Scanner in = new Scanner(System.in);
a = in.nextInt();
b = in.nextInt();
if (a < b) {
temp = a;
a = b;
b = temp;
}
m = fun1(a, b);
System.out.println("最大公约数为:" + m);
System.out.println("最小公倍数为:" + (a * b) / m);
}
}

你把返回操作 写到 循环者颂体 里面.
也就是 如果 进入不了循首闷郑环体,那这个fun1方法就没有返回了.

这给你在方法头定义的 punlic int .... int是制定返回类型罩迟