爱问知识人 爱问教育 医院库

java作业求助!!!!!!!!

首页

java作业求助!!!!!!!!

编一个程序Average.java,求从命令行输入的若干个正整数平均值的整数部分。要求:1)自定义一个异常类NotPositiveException,继承自Exception。当某个命令行参数是负整数或零时,生成并抛出一个这种异常的对象。2)程序能够捕获NumberFormatException,ArithmeticException,NotPositiveException,如果有其它异常也能使程序正常结束不被中断(提示:需要捕获Exception异常)3)用整型变量来作累加和,所作的运算也是整除运算。

提交回答
好评回答
  • 2007-11-05 22:43:05
      import java。io。BufferedReader;
    import java。io。InputStreamReader;
    import java。io。IOException;
    public class Average{
    	public static void main(String[] args) throws IOException{
    		while(true){
    			System。
      out。println("please input numbers, put space to split them"); String input = getSNumbers(); int numbers[] = new int[0]; try{ numbers = getNumbers(input); }catch(NumberFormatException e){ System。
      out。println("please input positive numbers"); continue; }catch(NotPositiveException e){ System。out。println(e。
      getMessage()); continue; } int total = 0; for(int i=0; i  length; System。out。println(result); }catch(ArithmeticException e){ System。out。println("don't input nothing"); } } } public static int[] getNumbers(String numbers){ String []sNumbers = numbers。
      split(" "); if(sNumbers。length==1&&sNumbers[0]。equals("")) return new int[0]; int[] iNumbers = new int[sNumbers。length]; for(int i=0; i  length; i++){ int temp = Integer。parseInt(sNumbers[i]); if(temp<=0) throw new NotPositiveException(temp + " is not positive"); iNumbers[i] = temp; } return iNumbers; } public static String getSNumbers()throws IOException{ BufferedReader reader = new BufferedReader(new InputStreamReader(System。
      in)); String input = reader。
      readLine(); return input; } } class NotPositiveException extends RuntimeException{ public NotPositiveException(String s){ super(s); } }。

    金***

    2007-11-05 22:43:05

类似问题

换一换

相关推荐

正在加载...
最新问答 推荐信息 热门专题 热点推荐
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200

热点检索

  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 177-196
返回
顶部
帮助 意见
反馈

确定举报此问题

举报原因(必选):