package cn.zheng; import java.util.Scanner; public class Demo { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Inpu ip = new Inpu(); Sushu ss = new Sushu(ip.run()); ss.run(); } }
package cn.zheng; import java.util.Scanner; public class Sushu { private int x; private int y; private int[] b; public Sushu(int[] b) { super(); this.b = b; } public Sushu() { super(); } public void run() { if(b == null) { return; } x = b[0]; y = b[1]; int temp = 0; if(x<y) { temp = x; x = y; y = temp; } while(x%y!=0) { temp = y; y=x%y; x=temp; } System.out.println("这两个数的最大公约数为:"+y); } }
package cn.zheng; import java.util.Scanner; public class Inpu { private int x; private int y; private int[] b; public int[] run() { b = new int[2]; int result; Scanner a = new Scanner(System.in); System.out.println("请输入两个整数"); try { System.out.println("请输入第一个整数"); x = Integer.parseInt((a.nextLine().replace(" ", ""))); System.out.println("请输入第二个整数"); y = Integer.parseInt((a.nextLine().replace(" ", ""))); if(x==0 || y==0) { System.out.println("您输入的数据不合法"); b=null; return b; } b[0]=x; b[1]=y; return b; } catch (Exception e) { // TODO: handle exception System.out.println("您输入的数据不合法"); b=null; return b; } } }这个类不知道如何用Junit测试···等我再学习一下再来修改···
package cn.zheng; import static org.junit.Assert.*; import org.junit.After; import org.junit.Before; import org.junit.Test; public class TestSushu { private Sushu ss; @Before public void setUp() throws Exception { ss=new Sushu(); } @After public void tearDown() throws Exception { } @Test public void test() { int []b = new int[2]; b[0]=34; b[1]=2; ss=new Sushu(b); ss.run(); b=null; ss=new Sushu(b); ss.run(); b=new int[2]; b[0]=2; b[1]=34; ss=new Sushu(b); ss.run(); b[0]=-35; b[1]=3; ss=new Sushu(b); ss.run(); } }
测试用例:第一个数: 3 6 第二个数:6 结果为6
第一个数: 结果为数据不合法
第一个数:-3 第二个数:34 结果为1
第一个数: a v c 结果为数据不合法