2022-3-25

1、題目內容:
从键盘输入一个整形数n,如果输入正确的话,输出1-n的值,如果输入错误的话输出“not int”
最后输出end
输入输出说明:
输入:
asd
输出:
not int
end

public class InputEx {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个整型");
        try {
            int num = sc.nextInt();
            for (int i = 1; i <= num; i++) {
                System.out.print(i + " ");
            }
        } catch (InputMismatchException e) {
            System.out.print("not int");
        } finally {
            System.out.println("\nend");
        }
    }
}

2、題目內容:
模拟向货船上装载集装箱,每个集装箱有一定重量,该重量(整数)大于100小于1000,货船总重为1000,
装载若干集装箱后,如果货船超重,那么货船认为这是一个异常,将拒绝装载集装箱,
但无论是否发生异常,货船都需要正点启航。

public class Test {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请以依次输入集装箱重量");
        boat b = new boat();
        int n = 0;
        try {
            while (true) {
                n = sc.nextInt();
                if (n < 100) {
                    System.out.println("集装箱太轻,请重新装箱!");
                    continue;
                }
                b.loding(n);
                System.out.println("轮船总重量为:" + b.num);
            }
        } catch (OverLoadException e) {
            e.message();
        } finally {
            System.out.println("正点启航");
        }
    }
}
class OverLoadException extends Exception {
    public void message() {
        System.out.println("超载");
    }
}
class boat {
    int num = 0;
    int maxnum = 1000;
    public void loding(int n) throws OverLoadException {
        num += n;
        if (maxnum < num) throw new OverLoadException();
    }
}

3、递归删除某个目录下的所有的文件
提示: delete()方法只能删除文件,不能删除文件夹
模仿课上讲的遍历指定目录及其子目录的方式

public class DeleteFile {
    public static void main(String[] args) {
        String path = "C:/Users/17974/Desktop/个人博客 +70/test";
        File f1 = new File(path);
        delete(f1);
    }
    public static void delete(File file) {
        //获取所有文件
        File[] files = file.listFiles();
        for (File f : files) {
            if (f.isFile()) {
                f.delete();
                System.out.println(f);
            } else {
                delete(f);
                System.out.println(f);
            }
        }
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值