public class MyException{?static int avg(int number1,int number2)throws MyException{??if(number1<0||number2<0){???throw new MyException("不可以使用負數(shù)");??}??if(number1>100||number2>100){???throw new MyException("數(shù)值太大");??}??return (number1+number2)/2;?}?public static void main(String[] args) {??// TODO Auto-generated method stub??try{???int result=avg(102,150);???System.out.println(result);??}catch(MyException e)??{???System.out.println(e);??}?}}
2 回答

miszhou
TA貢獻11條經(jīng)驗 獲得超1個贊
public class Test {
static int avg(int number1,int number2)throws Exception{
?if(number1<0||number2<0){
? throw new Exception("不可以使用負數(shù)");
?}
?if(number1>100||number2>100){
? throw new Exception("數(shù)值太大");
?}
?return (number1+number2)/2;
}
public static void main(String[] args) {
?// TODO Auto-generated method stub
?try{
? int result=avg(102,150);
? System.out.println(result);
?}catch(Exception e)
?{
?e.printStackTrace();
? System.out.println(e);
?}
}
}
試試這個,應(yīng)該可以
添加回答
舉報
0/150
提交
取消