//最大公约数 最小公倍数 通过测试 public class GongYue{ public static int gongyue(int m, int n) throws Exception{ if(m<1||n<1) throw new Exception("输入错误!"); while(m % n != 0) { int temp = m % n; m = n; n = temp; } return n; } //求m和n的最小公倍数 public static int gongbei (int m, int n) throws Exception{ return m * n / gongyue(m, n); } //----------------------------------------------------------------------------- public static void main(String[] args){ try{ int gy=gongyue(-1,25); System.out.println("gy="+gy); int gb=gongbei(1,10); System.out.println("gb="+gb); }catch(Exception e){ //e.printStackTrace(); System.out.println(e.getMessage()); //System.out.println(e.toString()); return;//catch中有return语句时,只有finally可以保证over的打印 } finally{//如果没有finally,直接写打印,则不会打印over。因为上面的return System.out.println("over"); } } }