使用idea写代码
首先创建了2个class文件
然后在hello文件里写判断最大公约数的方法,代码如下
public class hello {
public static int max(int a, int b){
int maxx = 0;
for(int j = 1;j <= a; j++){
if(a%j == 0 & b%j == 0){
if(maxx < j)
maxx = j;
}
}
return maxx;
}
}
然后我在test文件里调用该方法
public class test {
public static void main(String[] args){
System.out.println(hello.max(14,7));
}
}
调用结果