曾经连写这么一个东西都觉得很吃力。后来看了黑马的视屏,跟老师学了之后,觉得豁然开朗!
class commennum
{
public static void main(String[] args)
{
int number1=4;
int number2=12;
System.out.println("最大公约数是:"+maxcommendiv(number1,number2));
System.out.println("最小公倍数是:"+mincommenmul(number1,number2));
}
public static int maxcommendiv(int a,int b)
{
int r;
while (b!=0)
{
r=a%b;
a=b;
b=r;
}
return a;
}
public static int mincommenmul(int a,int b)
{
int max,min,i;
max=a>b?a:b;
min=a<b?a:b;
for(i=1;i<min;i++)
{
if (max*i%a==0&&max*i%b==0)
{
return max*i;
}
}
return a*b;
}
}
|