第一道题:
//后面的一一道来啊 别急,因为我也是新手,相互学习嘛!嗬嗬嗬嗬
import javax.swing.JOptionPane;
public class CommonDivisor {
public static void main(String[] args) {
String number1 = JOptionPane.showInputDialog("Enter an integer!");
String number2 = JOptionPane.showInputDialog("Enter an integer!");
int n = Integer.parseInt(number1);
int m = Integer.parseInt(number2);
int i = divisor(n, m);
int j = multiple(n, m);
String output = "The common divisor is " + i + "\n"
+ "The common multiple is " + j;
JOptionPane.showMessageDialog(null, output);
}
public static int divisor(int num1, int num2) {
int gcd = 1;
int k = 1;
while (k <= num1 && k <= num2) {
if (num1 % k == 0 && num2 % k == 0)
gcd = k;
k++;
}
return gcd;
}
public static int multiple(int num3, int num4) {
int gcd1 = 1;
//int x = 1;
for (int x =num3 * num4; x >=1; x--) {
if (x % num3 == 0 && x % num4 == 0){
gcd1 = x;
//return gcd1;
}
}
return gcd1;}
}