码迷,mamicode.com
首页 > 其他好文 > 详细

软件测试第二实验三作业(求最大公约数)

时间:2016-04-05 02:01:37      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:

package divisormax;

import java.util.Scanner;

public class Demo {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  Scanner s=new Scanner(System.in);
  System.out.println("请输入两个整数");
  int a=1;
  int b=1;
  try {
   a=s.nextInt ();
   b=s.nextInt();
  } catch (Exception e) {
   // TODO: handle exception
   System.out.println("您输入不是数字无法求公约数!");
  }
  
  MaxDivisor m=new MaxDivisor(a,b);
 }

}

 

 

 

package divisormax;

public class MaxDivisor{
 int min;
 int herf=0;
 
 
 public  MaxDivisor(int a,int b){
  if(a>b)
  {
   min=b;
  }
  else
  {
   min=a;
  }
  
  for(int i=1;i<=min;i++)
  {
   if(min%i==0)
   {
    if(((a+b)-min)%i==0)
    {
     herf=i;
    }
   }
  }
  System.out.println(herf);
 }
 
}

软件测试第二实验三作业(求最大公约数)

标签:

原文地址:http://www.cnblogs.com/hpj1038361347/p/5353534.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!