/**
* 1、題目內容:
* 从键盘输入一个整形数n,如果输入正确的话,输出1-n的值,如果输入错误的话输出“not int”
* 最后输出end
*
* 输入输出说明:
* 输入:
* asd
* 输出:
* not int
* end
*/
System.out.println("请输入数字:");
Scanner sc=new Scanner(System.in);
try {
int a1=Integer.parseInt(sc.next());
//Integer.parseInt()为将()内内容转换为整数
// 遇到一些不能被转换为整型的字符时,会抛出异常。
System.out.println(1-a1);
}catch (NumberFormatException e){
System.out.println("not int");
}finally {
System.out.println("end");
}
//next()从遇到第一个有效字符(非空格、换行符)开始扫描,遇到第一个分隔符或结束符(空格’ ‘或者换行符 ‘\n’)时结束。 nextLine()则是扫描剩下的所有字符串知道遇到回车为止。
Scanner sc = new Scanner(System.in); //加入输入的是:aaa bbb ccc str1=sc.next(); //str1="aaa" str2=sc.nextline(); //str2=" bbb ccc"
2、題目內容: 模拟向货船上装载集装箱,每个集装箱有一定重量,该重量(整数)大于100小于1000,货船总重为1000, 装载若干集装箱后,如果货船超重,那么货船认为这是一个异常,将拒绝装载集装箱, 但无论是否发生异常,货船都需要正点启航。
Scanner sc=new Scanner(System.in);
int chuan=1000;
int i = sc.nextInt();
int a=0;
chuan=chuan+i*1000;
try {
if (chuan>11000)
{
a=5/0;
}
if (chuan<11000)
{
a=5+5;
}
System.out.println("不超载。");
}catch (ArithmeticException e)
{
System.out.println("超重!");
}finally {
System.out.println("出发!");
}
3、递归删除某个目录下的所有的文件 提示: delete()方法只能删除文件,不能删除文件夹 模仿课上讲的遍历指定目录及其子目录的方式
String str="C:\\Users\\86177\\Desktop\\xxx";
File file=new File(str);
DelectTxt(file);
}
public static File[] DelectTxt(File file){
File[]files=file.listFiles();
for (int i = 0; i < files.length; i++) {
if (files[i].isFile())
{
files[i].delete();
}
if (files[i].isDirectory())
{
DelectTxt(files[i]);
}
}
return files;