1.jmu-Java-06异常-01-常见异常
main方法
- 事先定义好一个大小为5的数组。
- 根据屏幕输入产生相应异常。
提示:可以使用System.out.println(e)
打印异常对象的信息,其中e为捕获到的异常对象。
输入说明:
arr
代表产生访问数组是产生的异常。然后输入下标,如果抛出ArrayIndexOutOfBoundsException
异常则显示,如果不抛出异常则不显示。null
,产生NullPointerException
cast
,尝试将String对象强制转化为Integer对象,产生ClassCastException
。num
,然后输入字符,转化为Integer,如果抛出NumberFormatException
异常则显示。- 其他,结束程序
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
int [] arr = new int[5];
Scanner sc = new Scanner(System.in);
String input;
while(sc.hasNext()) {
input = sc.next();
try {
switch (input) {
case "arr":
int index = sc.nextInt();
if(index < 0 || index >= arr.length){
throw new ArrayIndexOutOfBoundsException("Index "+ index+ "out of bounds for length"+ arr.length);
}
case "null":
throw new NullPointerException();
case "cast":
Object str = "String";
Integer num = (Integer)str;
break;
case "num":
String da = sc.next();
int data = Integer.parseInt(da);
break;
case "other":
break;
}
}
catch (ArrayIndexOutOfBoundsException e) {
// TODO: handle exception
System.out.println(e);
}
catch (NullPointerException e) {
// TODO: handle exception
System.out.println(e);
}catch(ClassCastException e) {
System.out.println(e);
}
catch (NumberFormatException e) {
// TODO: handle exception
System.out.println(e);
}
}
}
}
2.jmu-Java-06异常-02-使用异常机制处理异常输入
main方法:
- 输入n,创建大小为n的int数组。
- 输入n个整数,放入数组。输入时,有可能输入的是非整型字符串,这时候需要输出异常信息,然后重新输入。
- 使用
Arrays.toString
输出数组中的内容。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
int [] arr = new int[5];
Scanner sc = new Scanner(System.in);
String input;
while(sc.hasNext()) {
input = sc.next();
try {
switch (input) {
case "arr":
int index = sc.nextInt();
if(index < 0 || index >= arr.length){
throw new ArrayIndexOutOfBoundsException("Index "+ index+ "out of bounds for length"+ arr.length);
}
case "null":
throw new NullPointerException();
case "cast":
Object str = "String";
Integer num = (Integer)str;
break;
case "num":
String da = sc.next();
int data = Integer.parseInt(da);
break;
case "other":
break;
}
}
catch (ArrayIndexOutOfBoundsException e) {
// TODO: handle exception
System.out.println(e);
}
catch (NullPointerException e) {
// TODO: handle exception
System.out.println(e);
}catch(ClassCastException e) {
System.out.println(e);
}
catch (NumberFormatException e) {
// TODO: handle exception
System.out.println(e);
}
}
}
}
3.成绩录入时的及格与不及格人数统计
编写一个程序进行一个班某门课程成绩的录入,能够控制录入成绩总人数,对录入成绩统计其及格人数和不及格人数。设计一个异常类,当输入的成绩小0分或大于100分时,抛出该异常类对象,程序将捕捉这个异常对象,并调用执行该异常类对象的toString()方法,该方法获取当前无效分数值,并返回一个此分数无效的字符串。
输入格式:
从键盘中输入学生人数n
从键盘中输入第1个学生的成绩
从键盘中输入第2个学生的成绩
从键盘中输入第n个学生的成绩
(注:当输入的成绩无效时(即分数为小于0或大于100)可重新输入,且输出端会输出此分数无效的提醒。)
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int jigeCount = 0;
for (int i = 0; i < n; i++) {
int grade = sc.nextInt();
if(grade < 0 || grade > 100) {
System.out.println(grade + "invalid!");
}
else if(grade >= 60) {
jigeCount ++;
}
}
System.out.println(jigeCount);
System.out.println(n-jigeCount);
}
}
4.jmu-Java-06异常-03-throw与throws
题目内容
编写类ArrayUtils
方法:public static double findMax(double[] arr,int begin, int end)
方法功能:用来返回arr数组中在下标begin与end-1之间(包括end-1)的最大值。
注意:必须使用throws关键字声明findMax。
方法说明:
- 要求begin<end,否则抛出相应的异常(
IllegalArgumentException
)。 - begin不得小于0,end不得大于arr.length,否则也抛出异常。
注意:抛出异常时,应让用户知道错误发生的原因。
main方法:
- 输入n,创建大小为n的int数组。
- 输入n个整数,放入数组。
- 输入若干对整数,代表begin与end,然后调用
ArrayUtils.findMax
方法。当输入的不是整数类型字符串,则退出输入循环。
package com.atg.demp8;
import java.util.Scanner;
class ArrayUtils{
public static double findMax(double[] arr,int begin, int end)throws IllegalArgumentException{
if(begin>=end) {
throw new IllegalArgumentException("begin:"+begin+" >= end:"+end);
}
else if(begin<0){
throw new IllegalArgumentException("begin:"+begin+" < 0");
}
else if(end>arr.length){
throw new IllegalArgumentException("end:"+end+" > arr.length");
}
double max=0;
for(int i=begin;i<end;i++){
if(max<arr[i])
max=arr[i];
}
return max;
}
}
public class demo3{
public static void main(String argn[]){
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
double[] arr =new double[n];
for(int i=0;i<n;i++){
arr[i]=scan.nextInt();
}
ArrayUtils beans = new ArrayUtils();
while(true){
int begin,end;
int m=0;
try{
begin = Integer.parseInt(scan.next());
end = Integer.parseInt(scan.next());
} catch(NumberFormatException e){
break;
}
try {
System.out.println(beans.findMax(arr,begin,end));
} catch (IllegalArgumentException e) {
System.out.println(e.toString());
}
}
try {
System.out.println(ArrayUtils.class.getDeclaredMethod("findMax", double[].class,int.class,int.class));
} catch (Exception e1) {
}
}
}