PTA练习 异常处理

7-1 jmu-Java-06异常-01-常见异常 (5 分)

自己编码以产生常见异常。

main方法:

  1. 事先定义好一个大小为5的数组。
  2. 根据屏幕输入产生相应异常。

提示:可以使用System.out.println(e)打印异常对象的信息,其中e为捕获到的异常对象。

输入说明:

  1. arr 代表产生访问数组是产生的异常。然后输入下标,如果抛出ArrayIndexOutOfBoundsException异常则显示,如果不抛出异常则不显示。
  2. null,产生NullPointerException
  3. cast,尝试将String对象强制转化为Integer对象,产生ClassCastException
  4. num,然后输入字符,转化为Integer,如果抛出NumberFormatException异常则显示。
  5. 其他,结束程序。

输入样例:

arr 4
null
cast
num 8
arr 7
num a
other

输出样例:

java.lang.NullPointerException
java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
java.lang.ArrayIndexOutOfBoundsException: 7
java.lang.NumberFormatException: For input string: "a"

 

import java.util.*;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int []a = new int [5];
		while(true)
		{
			String first;
			first = sc.next();
			if(first.equals("other"))
				break;
			if(first.equals("arr"))
			{
				try {
					int secend = sc.nextInt();
					int t = a[secend];
				}catch(Exception e){
					System.out.println(e);
				}
			}
			if(first.equals("null"))
			{
				try {
					String t = null;
					int length = t.length();
				}catch(Exception e) {
					System.out.println(e);
				}
			}
			if(first.equals("cast"))
			{
				try {
					Object ss = new String("string");
					System.out.println((Integer)ss);
				}catch(Exception e){
					System.out.println(e);
				}
			}
			if(first.equals("num"))
			{
				try {
					String c = sc.next();
					Integer num = Integer.parseInt(c);
				}catch(Exception e)
				{
					System.out.println(e);
				}
			}
 
		}
		sc.close();
	}
}

 

7-2 jmu-Java-06异常-02-使用异常机制处理异常输入 (5 分)

使用异常处理输入机制,让程序变得更健壮。

main方法:

  1. 输入n,创建大小为n的int数组。
  2. 输入n个整数,放入数组。输入时,有可能输入的是非整型字符串,这时候需要输出异常信息,然后重新输入。
  3. 使用Arrays.toString输出数组中的内容。

输入样例:

5
1
2
a
b
4
5
3

输出样例:

java.lang.NumberFormatException: For input string: "a"
java.lang.NumberFormatException: For input string: "b"
[1, 2, 4, 5, 3]
import java.util.*;
public class Main{
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int n=scan.nextInt();
		int a[]=new int[n];
		int count=0;
		while(count<n) {
			try {
				String temp=scan.next();
				int i=Integer.parseInt(temp);
				a[count++]=i;
			}catch(Exception e) {
				System.out.println(e);
			}
		}
		System.out.println(Arrays.toString(a));
		scan.close();
	}
}

 

7-3 成绩录入时的及格与不及格人数统计 (10 分)

编写一个程序进行一个班某门课程成绩的录入,能够控制录入成绩总人数,对录入成绩统计其及格人数和不及格人数。设计一个异常类,当输入的成绩小0分或大于100分时,抛出该异常类对象,程序将捕捉这个异常对象,并调用执行该异常类对象的toString()方法,该方法获取当前无效分数值,并返回一个此分数无效的字符串。

输入格式:

从键盘中输入学生人数n

从键盘中输入第1个学生的成绩

从键盘中输入第2个学生的成绩

...

从键盘中输入第n个学生的成绩

(注:当输入的成绩无效时(即分数为小于0或大于100)可重新输入,且输出端会输出此分数无效的提醒。)

输出格式:

显示及格总人数

显示不及格总人数

输入样例:

在这里给出一组输入。例如:

3
100
30
60

结尾无空行

输出样例:

在这里给出相应的输出。例如:

2
1

结尾无空行

输入样例:

在这里给出一组输入。例如:

2
200
69
30

结尾无空行

输出样例:

在这里给出相应的输出。例如:

200invalid!
1
1

结尾无空行

import java.util.*;
class MyException extends Exception
{
 MyException(int s)
 {
  super(s+"invalid!");
 }
}
public class Main{
public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int n =in.nextInt();
   int count1=0,count2=0;
   ArrayList<Integer> c = new ArrayList <> (n);
   for(int j=0;j<n;j++)
   {
    int z= in.nextInt();
    c.add(z);
    if(z>100||z<0)
     n++;
   }  
   for(int a:c)
 {
     try{
       if(a>100||a<0)
        throw new MyException(a);
       if(a<=100&&a>=60)
        count1++;
       if(a>=0&&a<60)
        count2++;
      }
     catch(MyException e)
     {
      System.out.println(e.getMessage());
     }
 }
 System.out.println(count1);
    System.out.println(count2);
}
}

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值