目录
7-2 jmu-Java-06异常-02-使用异常机制处理异常输入
7-3 jmu-Java-06异常-04-自定义异常(综合)
7-1 jmu-Java-06异常-01-常见异常
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
while(cin.hasNext())
{
String s = cin.next();
if(s.equals("other")) break;
if(s.equals("arr"))
{
int n = cin.nextInt();
if(n> 4 || n < 0)
System.out.println("java.lang.ArrayIndexOutOfBoundsException: "+n);
}
else if(s.equals("null"))
{
System.out.println("java.lang.NullPointerException");
}
else if(s.equals("cast"))
{
System.out.println("java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer");
}
else if(s.equals("num"))
{
s = cin.next();
char n = s.charAt(0);
if(n < '0' || n > '9')
System.out.println("java.lang.NumberFormatException: For input string: \"" + (char)n + "\"");
}
}
}
}
7-2 jmu-Java-06异常-02-使用异常机制处理异常输入
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int n = cin.nextInt();
cin.nextLine();
int[] a = new int[n];
for(int i = 0; i < n; i ++)
{
String s = cin.nextLine();
int flag = 1;
for(int j = 0; j < s.length(); j ++)
{
if(s.charAt(j) > '9' || s.charAt(j) < '0')
{
flag = 0;
break;
}
}
if(flag==0)
{
System.out.println("java.lang.NumberFormatException: For input string: \"" + s + "\"");
i --;
}
else
{
a[i] = Integer.parseInt(s);
}
}
System.out.println(Arrays.toString(a));
}
}
7-3 jmu-Java-06异常-04-自定义异常(综合)
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
while(cin.hasNext())
{
String s = cin.nextLine();
if(s.equals("other")) break;
s = cin.nextLine();
String str[] = s.split("\\s+");
if(str.length != 2)
{
System.out.println("java.util.NoSuchElementException");
}
else if(str[0].charAt(0) >= '0' && str[0].charAt(0) <= '9')
{
System.out.println("IllegalNameException: the first char of name must not be digit, name=" + str[0]);
int n = Integer.parseInt(str[1]);
if(n > 100)
{
System.out.println("IllegalScoreException: score out of range, score=" + n);
}
}
else
{
int n = Integer.parseInt(str[1]);
if(n > 100 || n < 0)
{
System.out.println("IllegalScoreException: score out of range, score=" + n);
}
else System.out.println("Student [name=" + str[0] + ", score=" + n + "]");
}
}
System.out.println("scanner closed");
}
}
7-4 天不假年
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int a = cin.nextInt();
int b = cin.nextInt();
if(a > b)
{
System.out.println("B");
}
else System.out.println("A");
}
}
7-5 成绩录入时的及格与不及格人数统计
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int n = cin.nextInt();
int bu = 0, ji = 0;
for(int i = 1; i <= n; i ++)
{
int m = cin.nextInt();
if(m < 0 || m > 100)
{
System.out.println(m + "invalid!");
i --;
}
else
{
if(m < 60)
bu ++;
else ji ++;
}
}
System.out.println(ji);
System.out.println(bu);
}
}
7-6 较为复杂情况下的求和
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int sum = 0;
String str = cin.nextLine();
String[] t = str.split("\\s+");
for(int j = 0; j < t.length; j ++)
{
String s = t[j];
int flag = 1;
for(int i = 0; i < s.length(); i ++)
{
char now = s.charAt(i);
if(now < '0' || now > '9')
{
flag = 0;
break;
}
}
if(flag == 1)
{
sum += Integer.parseInt(s);
}
}
System.out.println(sum);
}
}