java作业求助!!!!!!!!
编一个程序Average.java,求从命令行输入的若干个正整数平均值的整数部分。要求:1)自定义一个异常类NotPositiveException,继承自Exception。当某个命令行参数是负整数或零时,生成并抛出一个这种异常的对象。2)程序能够捕获NumberFormatException,ArithmeticException,NotPositiveException,如果有其它异常也能使程序正常结束不被中断(提示:需要捕获Exception异常)3)用整型变量来作累加和,所作的运算也是整除运算。
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; ilength; 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; ilength; 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); } }。
答:不知道你学到什么程度。学没学GUI啊?Swing呢?JDBC连接数据库呢? 1、创建一个父类和子类,父类有一个数据成员,子类继承父类的数据成员。通过构造函数初始...详情>>
答:google "java 缓冲池"详情>>
答:百度里搜张孝祥 非常好的视频教程详情>>