第五周技术博客~

这周学了类的多态,内部类,还有异常处理 其实就类的多态稍微比较熟练,内部类正在巩固,异常处理会简单的语法规则和阅读,处理方法不费很多 下面就是一一个题目,有用到|->类数组                          |->异常抛出,捕捉,处理                           题目貌似是创建三十个学生,有学号,有随机60-100的分数,最后根据大小排列后输出import java.util.ArrayList; import java.util.Scanner; class FailException extends Exception { FailException(String message) { super(message); } } class Student { private int stunum; private int score; Student(int stunum) { this.stunum = stunum; } void setScore() throws FailException //设置随机的分数 { this.score = (int)Math.round(Math.random()*40+60); if(score<60) throw new FailException("输入的分数不及格"); } void setScore(int num) throws FailException//设置分数 { if(num<60) throw new FailException("输入的分数不及格"); this.score = num; } static boolean compare(Student i, Student j)  // 比较学生分数的大小 { if(i.score>j.score) return true; else return false; } void getScore() //获得分数和学号 { System.out.println(this.score+"\t\t"+this.stunum); } } @SuppressWarnings("unchecked") class  StuScore  { public static void main(String[] args) { /* ArrayList stuscore = new ArrayList(); for(int i = 20070301; i<20070330;i++) { stuscore.add(new Student(i)); stuscore.get(i-20070301).setScore(); stuscore.get(i-20070301).getScore(); } */ int i; Student[] stu = new Student[30]; for (i = 1; i<=30 ; i++ ) // 初始化Student数组 { stu[i-1] = new Student(20070300+i); try { stu[i-1].setScore(); stu[i-1].getScore(); } catch (FailException e) { System.out.println(e.toString()); } } ArrayListSort.maxToMin(stu); try { stu[20].setScore(30); } catch (FailException e) { System.out.println(e.toString()); } for (i = 1; i<=30 ; i++ ) // 初始化Student数组 { stu[i-1].getScore(); } } } class ArrayListSort { public static void maxToMin(Student[] stu) { /* Student[] s = new Student[30]; Student mid; for(int i = 30; i>1 ;i-- ) for (int j=i-1; j>=1 ;j-- ) { if(stuscore.get(j).getScore()<stuscore.get(i).setscore();) mid="stuscore.get(j).getScore();" }="" *="" Student="" mid;="" System.out.println(?after="" sort?);="" for="" (int="" i="29;">=1 ; i-- ) { for (int j= i-1; j>=0 ; j-- ) { if (Student.compare(stu[i],stu[j])) { mid = stu[i]; stu[i]=stu[j]; stu[j]=mid; } } } } } /* 代码缺陷:在判断到输入错误分数时,无法进行异常嵌套.. 如果进行嵌套输入则又需要try进入无限循环。我将百度一下*/

posted on 2016-03-29 18:25  fanfans1996  阅读(107)  评论(0编辑  收藏  举报