直接上代码
…不然也没什么好讲的
public class MyException extends Exception{
String message;
MyException(){
message="输入的数不能为负数";
}
public String toString(){
return message ;
}
public class Student {
public static int maxCommonDivsor(int a,int b){
if(a<b){
int temp = a; a=b; b=temp;
}
while (a %b!=0){
int temp =a%b;
a=b;
b=temp;
}
return b;
}
public int speak (int m,int n) throws MyException{
if(m<0){
MyException ex =new MyException ();
throw ex;
}else if(n<0)
{
MyException ex =new MyException ();
throw ex;
}else{
return maxCommonDivsor( m, n);
}}}
import java.util.Scanner;
public class Test1 {
public static void main(String[] args) {
System.out.print("请输入第一个数:");
Scanner sca = new Scanner (System.in);
int x= sca.nextInt();
System.out.print("请输入第二个数:");
Scanner sca1 = new Scanner (System.in);
int y= sca.nextInt();
try{
Student stu = new Student();
stu.speak(x, y);
}catch (MyException e){
System.out.print(e.toString());
} } }