⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 exceptiontest.java

📁 关于java面向对象系统分析方面的课件
💻 JAVA
字号:
import java.util.*;

public class ExceptionTest {

    /**
     * 孙悟空吃蟠桃,王母娘娘善后
     */
    public static void main(String[] args) {

        int full = 0; // 孙悟空的胃口,100就饱了
        while (true) {
            int peach = new Random().nextInt() % 7; // 蟠桃的品质:0为烂桃,1为难吃的蟠桃,2为普通的蟠桃,其他为美味的蟠桃
            /* 孙悟空吃桃 */
            try {
                switch (peach) {
                case 0: // 烂桃不吃
                    System.out.println("孙悟空:这桃烂了,不能吃!");
                    throw new Exception();
                case 1: // 难吃的桃只吃一口(不吃怎知难吃)
                    System.out.println("孙悟空:这桃不好吃!(吃了一口)");
                    full += 1;
                    throw new NullPointerException();
                case 2: // 普通蟠桃只吃一半
                    System.out.println("孙悟空:这桃还行。(吃了一半)");
                    full += 5;
                    throw new ArithmeticException();
                default: // 美味蟠桃吃光
                    System.out.println("孙悟空:这桃太好吃了!(吃光了)\n");
                    full += 10;
                    break;
                }
                if (full >= 100) {
                    System.out.println("老孙吃饱了!(扬长而去)");
                    break;
                }
                /* 王母娘娘善后 */
            } catch (NullPointerException e) { // 只吃了一口的桃的处理
                System.out.println("王母娘娘:泼猴!(拾起被咬了一口的蟠桃)\n");
            } catch (ArithmeticException e) { // 半只桃的处理
                System.out.println("王母娘娘:你这猴头!(拾起半只蟠桃)\n");
            } catch (Exception e) { // 烂桃的处理
                System.out.println("王母娘娘:我的蟠桃 . . .(拾起烂桃)\n");
            }
        }
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -