第一次编程作业-求两个整数的最大公约数

来源:互联网 发布:javascript与jsp 编辑:程序博客网 时间:2024/05/01 10:18

(无名)

同学第一次编程作业原件

修改版:

package ch1;import java.io.IOException;import java.util.Scanner;public class Gcd {/*算法概论课第一周课外作业 * 编写计算“两个整数的最大公约数”程序。 *要求通过键盘输入两个整数、检查输入整数是否符合要求,并计算出其最大公约数。 */int  divisor( int  m, int  n) {        int  temp;        if (m < n) {           temp = m;           m = n;           n = temp;       }        while (m % n != 0 ) {           temp = n;           n = m % n;           m = temp;       }        return  n;   }public static void main(String args[]) throws IOException {        Scanner sc = new Scanner(System.in);        Gcd obj = new  Gcd();        System.out.print( "请输入第一个整数:" );        int a = sc.nextInt();        System.out.print( "请输入第二个整数:" );        int b = sc.nextInt();        System.out.println(a + "和" + b + "的最大公约数是:" + obj.divisor(a,b));    }}
运行结果:

运行结果

0 0
原创粉丝点击