20145106 《Java程序设计》第5周学习总结

教材学习内容总结

个人认为本周的学习在很大程度上是作为之前学习内容的补充。之前编译的程序相信所有人都会失败过,error算是我程序的老主顾了。
第八章名为“异常处理”。本章中,我们可以运用try、catch语句来捕捉异常并在错误发生时显示友好的错误信息。譬如说要输入“0”却误输入了“o”.

public class Average2 {
    public static  void main(String[] args){

        try{

            Scanner scanner = new Scanner(System.in);
            double sum = 0;
            int count = 0;
            int number;
            while(true){
                number = scanner.nextInt();
                if(number==0){

                    break;
                }
                sum+=number;
                count++;
            }

            System.out.printf("平均%.2f%n", sum/count);


        }catch (InputMismatchException ex){

            System.out.println("必须输入整数");
        }

    }

}

常用集合类的继承结构如下:
Collection<--List<--Vector
Collection<--List<--ArrayList
Collection<--List<--LinkedList
Collection<--Set<--HashSet
Collection<--Set<--HashSet<--LinkedHashSet
Collection<--Set<--SortedSet<--TreeSet
Map<--SortedMap<--TreeMap
Map<--HashMap

import static java.lang.System.out;

public class Guest
{
    public static void main(String[] args)
    {
        List names = new java.util.ArrayList();
        collectNameTo(names);
        out.println("访客名单:");
        printUpperCase(names); 
    }

    static void collectNameTo(List names) 
    {
        Scanner console = new Scanner(System.in);
        while(true)
        {
            out.print("访客名称:");
            String name = console.nextLine();
            if(name.equals("quit")) 
            {
                break;
            }
            names.add(name);
        }
    }

    static void printUpperCase(List names)
    {
        for(int i = 0; i < names.size(); i++) 
        {
           String name = (String) names.get(i);
            out.println(name.toUpperCase());
        }        
    }        
}

本周代码托管截图

学习进度条

代码行数(新增/累积) 博客量(新增/累积) 学习时间(新增/累积) 重要成长
目标 5000行 30篇 400小时
第一周 15/15 2/2 20/20
第二周 85/100 1/3 8/18
第三周 200/300 1/4 13/31
第四周 200/500 1/5 11/42
第五周 400/900 1/6 10/52

参考资料

posted @ 2016-04-03 23:49  20145106ssr  阅读(122)  评论(2编辑  收藏  举报