zoukankan      html  css  js  c++  java
  • 201521123060 《Java程序设计》第9周学习总结

    1.本周学习总结

    1.1 以你喜欢的方式(思维导图或其他)归纳总结异常相关内容。

    2.书面作业

    本次PTA作业题集异常

    1.常用异常
    题目5-1
    1.1截图你的提交结果(出现学号)

    1.2自己以前编写的代码中经常出现什么异常、需要捕获吗(为什么)?应如何避免?
    答:以前有出现过数组越界异常,不需要捕获,在编写代码时注意数组下标,不让数组下标越界就行了。

    1.3什么样的异常要求用户一定要使用捕获处理?
    答:除了Error与RuntimeException及其子类以外的异常代码中必须try-catch来捕获异常。

    2.处理异常使你的程序更加健壮
    题目5-2
    2.1 截图你的提交结果(出现学号)

    2.2 实验总结
    答:输入非整型字符串,抛出NumberFormatException异常,i--,重新输入,使得输入符合要求。

    3.throw与throws
    题目5-3
    3.1 截图你的提交结果(出现学号)

    3.2 阅读Integer.parsetInt源代码,结合3.1说说抛出异常时需要传递给调用者一些什么信息?

       public static int parseInt(String s) throws NumberFormatException {
            return parseInt(s,10);
        }
    

    答:需要传递给调用者是什么异常,产生异常的原因,如3.1中,如果begin>=end,抛出相应的异常IllegalArgumentException

    4.函数题
    题目4-1(多种异常的捕获)
    4.1 截图你的提交结果(出现学号)

    4.2 一个try块中如果可能抛出多种异常,捕获时需要注意些什么?
    答:子类异常必须放在父类异常前面。

    5.为如下代码加上异常处理

    byte[] content = null;
    FileInputStream fis = new FileInputStream("testfis.txt");
    int bytesAvailabe = fis.available();//获得该文件可用的字节数
    if(bytesAvailabe>0){
        content = new byte[bytesAvailabe];//创建可容纳文件大小的数组
        fis.read(content);//将文件内容读入数组
    }
    System.out.println(Arrays.toString(content));//打印数组内容
    

    5.1 改正代码,让其可正常运行。注1:里面有多个方法均可能抛出异常。注2:要使用finally关闭资源。

    byte[] content = null;
    		FileInputStream fis =null;
    		try{
    			fis= new FileInputStream("testfis.txt");
    			int bytesAvailabe = fis.available();//获得该文件可用的字节数
    			if(bytesAvailabe>0){
    			    content = new byte[bytesAvailabe];//创建可容纳文件大小的数组
    			    fis.read(content);//将文件内容读入数组
    			}
    		}catch(FileNotFoundException e){
    			System.out.println(e);
    		}catch(IOException e){
    			System.out.println(e);
    		}finally
            {
                if(fis!=null)
                    try{
                        fis.close();
                    }
                catch(Exception e){System.out.println(e);}
            }
    		System.out.println(Arrays.toString(content));//打印数组内容
    

    5.2 使用Java7中的try-with-resources来改写上述代码实现自动关闭资源.

    byte[] content = null;
    		try(FileInputStream fis= new FileInputStream("testfis.txt")){
    			int bytesAvailabe = fis.available();//获得该文件可用的字节数
    			if(bytesAvailabe>0){
    			    content = new byte[bytesAvailabe];//创建可容纳文件大小的数组
    			    fis.read(content);//将文件内容读入数组
    			}
    		}catch(FileNotFoundException e){
    			System.out.println(e);
    		}catch(IOException e){
    			System.out.println(e);
    		}
    		System.out.println(Arrays.toString(content));//打印数组内容
    

    6.重点考核:使用异常改进你的购物车系统(未提交,得分不超过6分)
    举至少两个例子说明你是如何使用异常处理机制让你的程序变得更健壮。
    说明要包含2个部分:1. 问题说明(哪里会碰到异常)。2.解决方案(关键代码)

    1.输入不在列表中的数字选择时,抛出异常。

    System.out.println("输入想要购买的商品");
    		System.out.println("1.书");
    		System.out.println("2.食品");	
    		try{
    		int n=sc.nextInt();
    		if(n==1)
    			{System.out.println("1"+book[0]);
                 System.out.println("2"+book[1]);
                 System.out.println("输入有效数字选择商品");
                 int i=sc.nextInt();
                 System.out.println("输入想要购买数量");
                 int x=sc.nextInt();
                 Shoppingcart p=new Shoppingcart(book[i-1],x);
                 cart[0]=p;
    			}
    		else if(n==2)
    		    {System.out.println("1"+food[0]);
    	        System.out.println("2"+food[1]);}
    		}catch (InputMismatchException e){
    			System.out.println(e);
    		}
    

    2.添加商品数量时,手误输入的不是数字,抛出异常。

     System.out.println("请您输入要添加的数量");
    		 try{
    		 int y=sc.nextInt();
    		 cart[0].add(y, o);
    		 }catch (InputMismatchException e){
    			 System.out.println(e);
    		 }
    

    7.选做:JavaFX入门
    如果未完成作业1、2的先完成1、2。贴图展示。如果已完成作业1、2的请完成作业3。内有代码,可在其上进行适当的改造。建议按照里面的教程,从头到尾自己搭建。

    8.选做:课外练习
    JavaTutorial中Questions and Exercises
    练习总结

    3.码云上代码提交记录

    题目集:异常
    3.1 码云代码提交记录
    在码云的项目中,依次选择“统计-Commits历史-设置时间段”, 然后搜索并截图

  • 相关阅读:
    LintCode "Maximum Gap"
    LintCode "Wood Cut"
    LintCode "Expression Evaluation"
    LintCode "Find Peak Element II"
    LintCode "Remove Node in Binary Search Tree"
    LintCode "Delete Digits"
    LintCode "Binary Representation"
    LeetCode "Game of Life"
    LintCode "Coins in a Line"
    LintCode "Word Break"
  • 原文地址:https://www.cnblogs.com/zq1996/p/6748701.html
Copyright © 2011-2022 走看看