目录
第一关
第二关
package step2;
import java.util.Scanner;
public class Task {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num1 = sc.nextInt();
int num2 = sc.nextInt();
/********* Begin *********/
try {
int res = num1 / num2;
} catch (ArithmeticException e) {
System.out.println("除数不能为0");
}
System.out.println(num1/num2);
/********* End *********/
}
}
第三关
package step5;
import java.util.InputMismatchException;
import java.util.Scanner;
public class ExceptionDemo {
public static void main(String[] args) {
// *******定义一个静态数组,为确保输出结果正确,请指定数组元素为:{23, 56, 789, -12, 35, 67, 89, 75, 66, 32} ******//
int[] a = {23, 56, 789, -12, 35, 67, 89, 75, 66, 32};
// *******调用静态方法search() ,从键盘输入一个索引号,根据索引号查找并输出数组的指定元素******//
// *******捕捉并处理捕捉并处理输入索引号类型不匹配异常( InputMismatchException )和数组下标越界异常(ArrayIndexOutOfBoundsException)******//
int idx = 0;
try {
Scanner sc = new Scanner(System.in);
idx = sc.nextInt();
search(a, idx);
} catch (InputMismatchException i) {
System.out.println("输入数据类型不匹配null");
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("输入的索引号越界" + idx);
}
System.out.println("查找到的元素是:" + search(a, idx));
}
// *******定义静态方法search(int [] a,int x),实现根据索引号x,在数组a中查找指定下标的元素并返回该元素
static int search(int[] a, int x) {
return a[x];
}
}
第四关
package step3;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class Task {
/********* Begin *********/
//请在合适的部位添加代码
public static void main(String[] args) throws FileNotFoundException {
test();
}
public static void test() throws FileNotFoundException {
File file = new File("abc");
if(!file.exists()){ //判断文件是否存在
//文件不存在,则 抛出 文件不存在异常
throw new FileNotFoundException("该文件不存在");
}else{
FileInputStream fs = new FileInputStream(file);
}
}
/********* End *********/
}
第五关
package step4;
import java.util.Scanner;
public class Task {
/********* Begin *********/
public static void main(String[] args) throws MyException {
Scanner sc = new Scanner(System.in);
String username = sc.next();
//判断用户名
if (username.length() < 3) {
throw new MyException("用户名小于三位Exception");
}
System.out.println("用户名格式正确");
}
}
class MyException extends Exception {
private static final long serialVersioUID = 1L;
public MyException(){
}
public MyException(String msg) {
super(msg);
}
}
/********* End *********/
第六关
自定义异常类:NoThisSongException
package step6;
public class NoThisSongException extends Exception{
// *******自定义播放异常类***********//
NoThisSongException(){}
NoThisSongException(String msg) {
super(msg);
}
}
Player
package step6;
public class Player {
// ********定义方法play(int index),根据指定的索引号播放对应歌曲,若索引号>10,则抛出“NoThisSongException”异常**********//
static void player(int index) throws NoThisSongException {
if (index <= 10) {
System.out.println("正在播放第" + index + "首歌曲");
} else if (index > 10){
throw new NoThisSongException("您播放的歌曲不存在");
}
}
}
PlayerTest
package step6;
import java.util.Scanner;
public class PlayerTest {
public static void main(String[] args) {
//******* 创建Player对象,并调用player()方法,捕获并处理该方法可能产生的异常 ********//
Scanner sc = new Scanner(System.in);
int index = sc.nextInt();
try {
Player.player(index);
} catch (NoThisSongException e) {
System.out.println(e.getMessage());
}
}
}