标签:ring 提交 rri false over contains header 9.png 记录
本次PTA作业题集异常
结合题集题目7-1回答
经常出现NullPointerException
、ArrayIndexOutOfBoundsException
、NumberFormatException
,这些异常均继承RuntimeException类,一般由程序错误导致,无需捕获。
应该在代码中对容易抛出异常的代码段特别注意,比如调用数组时注意不要越界,格式的转换调用正确的方法。
已检查异常(checked)一定要捕获,如文件处理时常见的FileNotFoundException
题集题目7-2
输入的可能是数字字符也可能是其他类型的字符,因此用Integer.parseInt(input)
处理输入时用try-catch
包围,输入不当时输出NumberFormatException
使用异常处理机制处理可能出现的异常能让程序更健壮
题集题目7-3
阅读Integer.parsetInt源代码
Integer.parsetInt源代码:
public static int parseInt(String s) throws NumberFormatException {
return parseInt(s,10);
}
public static int parseInt(String s, int radix)
throws NumberFormatException
{
if (s == null) {
throw new NumberFormatException("null");
}
if (radix < Character.MIN_RADIX) {
throw new NumberFormatException("radix " + radix +
" less than Character.MIN_RADIX");
}
if (radix > Character.MAX_RADIX) {
throw new NumberFormatException("radix " + radix +
" greater than Character.MAX_RADIX");
}
int result = 0;
boolean negative = false;
int i = 0, len = s.length();
int limit = -Integer.MAX_VALUE;
int multmin;
int digit;
if (len > 0) {
char firstChar = s.charAt(0);
if (firstChar < ‘0‘) { // Possible leading "+" or "-"
if (firstChar == ‘-‘) {
negative = true;
limit = Integer.MIN_VALUE;
} else if (firstChar != ‘+‘)
throw NumberFormatException.forInputString(s);
if (len == 1) // Cannot have lone "+" or "-"
throw NumberFormatException.forInputString(s);
i++;
}
multmin = limit / radix;
while (i < len) {
// Accumulating negatively avoids surprises near MAX_VALUE
digit = Character.digit(s.charAt(i++),radix);
if (digit < 0) {
throw NumberFormatException.forInputString(s);
}
if (result < multmin) {
throw NumberFormatException.forInputString(s);
}
result *= radix;
if (result < limit + digit) {
throw NumberFormatException.forInputString(s);
}
result -= digit;
}
} else {
throw NumberFormatException.forInputString(s);
}
return negative ? result : -result;
}
NumberFormatException
,当传入参数不是整型字符串时,提醒方法的调用者发生了什么错误要给提醒调用者发生了什么错误。
在3.1Integer.parseInt()中以下情况会分别抛出不同提示信息的NumberFormatException
1.传入字符串为null时
2.进制小于2或大于36时
3.首字符不是数字或‘+‘或‘-‘
4.只输入了‘+‘或‘-‘
5.有不是数字的字符
6.长度超出int范围
题集题目6-3
异常处理通常是防止未知错误产生所采取的处理措施。出现错误时程序不会显示错误,而是按照自己设计的错误处理方法进行处理。
可以不使用。也可以使用try-catch捕获异常。
好处:当想将此方法中可能出现的异常交给其他调用此方法的方法处理时,无需在方法中加入try-catch块,只要使用throws关键字声明该方法抛出的异常即可。
题集题目6-1
多个catch
放置时,不能把父类异常放在子类异常前。如本题要以NumberFormatException、IllegalArgumentException、Exception的顺序放置。
使用|
来分开异常,多个异常之间不能有继承关系
普通异常捕获:
catch (IOException ex) {
System.out.println(ex.getMessage());
}catch (SQLException ex) {
System.out.println(ex.getMessage());
}catch (Exception ex) {
System.out.println(ex.getMessage());
}
多重异常捕获语法:
catch(IOException | SQLException | Exception ex){
System.out.println(ex.getMessage());
}
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));//打印数组内容
注1:里面有多个方法均可能抛出异常。
功能2:需要添加finally关闭文件。无论上面的代码是否产生异常,总要提示关闭文件ing。如果关闭文件失败,提示关闭文件失败!
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.toString());
System.out.println("找不到文件testfis.txt,请重新输入文件名,然后尝试重新打开");
}
catch(Exception e) {
System.out.println(e);
System.out.println(e.getMessage()+"打开或读取文件失败!");
}
finally {
System.out.println("关闭文件ing");
if(fis!=null)
{
try {
fis.close();
}
catch (IOException e) {
System.out.println(e);
System.out.println("关闭文件失败!");
}
}
}
System.out.println(Arrays.toString(content));//打印数组内容
}}
将无论是否捕获异常都要进行的操作放在finally块,因为finally不管是否捕获都会运行。使用finally关闭资源时注意finally中的代码也有可能要用try-catch包围。
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.toString());
System.out.println("找不到文件testfis.txt,请重新输入文件名,然后尝试重新打开");
}
catch(Exception e) {
System.out.println(e);
System.out.println(e.getMessage()+"打开或读取文件失败!");
}
finally {
System.out.println("关闭文件ing");
}
System.out.println(Arrays.toString(content));
}}
使用try-with-resources时编译器自动在try-with-resources后面增加了判断对象是否为null,如果不为null,则调用close()函数的的字节码。
用编绎器生成的字节码的角度来看,try-with-resources语法更加高效点。并且实现资源的自动回收处理,大大提高了代码的便利性
登录lib.jmu.edu.cn,对图书进行搜索。然后登录图书馆信息系统,查看我的图书馆。如果让你实现一个图书借阅系统,尝试使用面向对象建模。
学生,教师
登录界面,图书信息展示,搜索图书,借阅
简单设计类图:
用Map储存图书信息,key中储存图书编号或名称,编写Book类存入value。
class Book{
private int id;
private String name;
private String author;
private String place;
private String Information;
private int num;
...
}
读者信息同样使用Map储存便于查询,key存学号或教师工号,value存Reader类
class Reader{
private int id;
private String name;
private String password;
private Set<Book>; //存已借阅书籍和数量
...
}
使用文本文件.txt来储存
图书信息.txt储存图书的名字、信息、剩余馆藏数
读者信息.txt储存读者的帐号、密码、借阅的书籍及数量
举1个例子说明你是如何使用异常处理机制让你的程序变得更健壮。
说明要包含2个部分:1. 问题说明(哪里会碰到异常)。2.解决方案(关键代码)
1.问题说明:删除购物车中的商品时,若购物车中没有商品,则会发生空指针异常。
2.解决方法:
使用try-catch包围代码,当发生空指针错误时,界面弹出消息框提示购物车中没有商品
1.覆写方法加上@Override
注释
2.if/else/while/do
语句中必须使用{}
3.变量名采用驼峰法命名
4.常量要定义才能使用
5..抽象类和方法要注释
题目集:异常
在码云的项目中,依次选择“统计-Commits历史-设置时间段”, 然后搜索并截图
周次 | 行数 | 新增行数 | 文件数 | 新增文件数 |
---|---|---|---|---|
1 | 226 | 226 | 45 | 45 |
2 | 377 | 377 | 7 | 7 |
3 | 712 | 281 | 42 | 9 |
4 | 166 | 166 | 2 | 2 |
5 | 253 | 253 | 4 | 4 |
6 | 484 | 484 | 9 | 9 |
7 | 269 | 269 | 1 | 1 |
8 | 301 | 32 | 5 | 4 |
9 | 447 | 447 | 7 | 7 |
10 | 401 | 26 | 9 | 0 |
协会签到,每次都是使用纸质签名表进行签到,回去后再手动统计。如果有多张签名表,需要手动统计。现在保持纸质签名方式不变,负责人员每次将纸质签名表待会后手动录入系统,这样就可实现数据的自动统计。实现这样的系统需包含什么功能模块?尝试编码实现?
如果有多张签名表,每张表之间的输入用"end"
隔开,统计end出现次数即为签名表数量。据此可统计出勤次数。
运行代码:
public class Attendance {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
Map<String,Integer> map=new HashMap<>();
int count=0;
while(true) {
String a=in.next();
if(a.equals("!!!!!"))
break;
if(a.equals("end"))
count ++;
else if(!a.equals("end") ) {
if(!map.containsKey(a)) {
map.put(a, 0);
}
if(map.containsKey(a)) {
map.put(a, map.get(a)+1);
}
}}
for(Map.Entry<String,Integer> m: map.entrySet()){
System.out.println("姓名: "+m.getKey()+" "+"出勤次数: "+m.getValue());
if(m.getValue()<count) {
System.out.println("缺勤次数:"+(count-m.getValue()));
}
if(m.getValue()==count) {
System.out.println("满勤");
}
}
}}
运存截图:
201621123063《java程序设计》第10周实验总结
标签:ring 提交 rri false over contains header 9.png 记录
原文地址:http://www.cnblogs.com/Yelz/p/7885692.html