家庭网络共享查看成员:Java问题1

来源:百度文库 编辑:高校问答 时间:2024/04/30 07:21:42
average()方法用于求三个数的平均数,它有三个整形形式参数,且此三个形式参数的取值范围均是〔0,100〕.倘若形式参数得到的值小于0或者大于100,程序就会抛出异常。请完成次average()方法。
int average(int a, int b, int c) throws RuntimeException{
//a<0,b<0,c<0,则抛出Arithmeticexception异常
//a>100,b>100,c>100,则抛出Illegalargumentexcption异常
return (a+b+c)/3;
}
朋友,我对你的程序看了一下,还不错,但是能通过编译,执行的时候却不能进行,麻烦你看看!

希望这个可以帮助你。
import java.io.*;
public class Main{
public static float everage(int a,int b,int c) throws MyException{
if(a<=0||b<=0||c<=0) throw new MyException("Arithmeticexception异常");
if(a>=100||b>=100||c>=100) throw new MyException("llegalargumentexcption异常");
else return (float)(a+b+c)/3;
}
public static void main(String args[])throws IOException{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Please Input Three Integers: ");
Main aMain=new Main();
int a=0;
int b=0;
int c=0;
boolean bool=true;
while(true){
try{
a=Integer.parseInt(br.readLine());
b=Integer.parseInt(br.readLine());
c=Integer.parseInt(br.readLine());
aMain.everage(a,b,c);
float result=aMain.everage(a,b,c);
System.out.println(result);
}
catch(MyException e){
System.out.println(e.getMessage());
}
}
}
}
class MyException extends Exception{
public MyException(){
super("Error!");
}
public MyException(String message){
super(message);
}
}

int average(int a, int b, int c) throws RuntimeException,Arithmeticexception,Illegalargumentexcption{
//a<0,b<0,c<0,则抛出Arithmeticexception异常
//a>100,b>100,c>100,则抛出Illegalargumentexcption异常
if(a<0||b<0||c<0) throw new Arithmeticexception();
if(a>100||b>100||c>100) throw new Illegalargumentexcption();
if((a>0&&a<100)&&(b>0&&b<100)&&(c>0&&c<100))
return (a+b+c)/3;
}
不知是否是你想要的,但愿能帮助你,祝好!