2022年10月07日Java上机

示例6-1

JBT6201
本例子演示的是异常处理,所以程序会报错
本例子演示的是异常处理,所以程序会报错
本例子演示的是异常处理,所以程序会报错

1
2
3
4
5
6
7
8
9
10
public class JBT6201 {
public static void main(String args[]) {
double num = 1;
try {
num = 1 / 0;
} catch (ArithmeticException e) {
e.printStackTrace();
}
}
}

示例6-8

JBT6602

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
class MoneyException extends Exception {
private ATM atm;
private double money;

MoneyException(ATM atm, double money) {
this.atm = atm;
this.money = money;
}

public String getExcepMessage() {
String str = "存款=" + atm.deposit + "<" + "支取=" + money;
return str;
}
}

class ATM {
double deposit;

ATM(double deposit) {
this.deposit = deposit;
}

public void saveMoney(double money) {
if (money > 0.0) deposit = deposit + money;
}

public void getMoney(double money) throws MoneyException {
if (deposit < money)
throw new MoneyException(this, money);
else {
System.out.println("取款成功:"+money);
}
deposit = deposit - money;
}

public void showMoney() {
System.out.println("存款=" + deposit);
}
}

public class JBT6602 {
public static void main(String args[]) {
ATM atm = new ATM(100);
try {
atm.getMoney(299);
} catch (MoneyException e) {
System.out.println(e.toString());
System.out.println(e.getExcepMessage());
}
}
}

扩展题

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class NumberDemo {
public static void main(String[] args) {
String[] numberLetter = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
Map<String, Integer> maps = new HashMap();
for (int i = 0; i < numberLetter.length; i++) {
maps.put(numberLetter[i], i);
}
Scanner input = new Scanner(System.in);
System.out.println("请输入英文数字,用逗号分割!");
String str = input.nextLine();
String[] inputStr = str.split(",");
try {
showNumber(maps, inputStr);
} catch (InputException e) {
System.out.println(e.getExcepMessage());
}
}

private static void showNumber(Map<String, Integer> maps, String[] inputStr) throws InputException {
int count = 0;
for (int i = 0; i < inputStr.length; i++) {
inputStr[i] = inputStr[i].toLowerCase();
if (maps.get(inputStr[i]) != null) {
count++;
}
}
if (inputStr.length != count) {
throw new InputException("输入异常!");
}
for (int i = 0; i < inputStr.length; i++) {
System.out.print(maps.get(inputStr[i]));
}
}
}

class InputException extends Exception {
private String message;
public InputException(String message) {
this.message = message;
}
public String getExcepMessage() {
return message;
}
}

文件操作一

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.FileTime;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

public class CreteDirDemo {
public static void main(String[] args) throws IOException {
String path = "C:\\MyDir\\";
//在mac系统中改为"/Users/用户名/MyDir/"
File file = new File(path);
if (!file.exists()) {
file.mkdir();
} else {
System.out.println("创建目录失败!!!目录已存在");
}
String fileName = path + "test.txt";
file = new File(fileName);
if (file.createNewFile()) {
System.out.println("创建文件成功");
BasicFileAttributes bfa = Files.readAttributes(Paths.get(fileName), BasicFileAttributes.class);
FileTime date = bfa.creationTime();
DateFormat df = new SimpleDateFormat("yyyy年MM月dd日");
String dateCreated = df.format(date.toMillis());
System.out.println("创建时间:" + dateCreated);
System.out.println("文件大小:" + bfa.size());
}
file.delete();
}
}

文件操作二

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import java.io.File;
public class TraverseDirDemo {
public static void main(String[] args) {
String path = "C:\\windows";
//mac系统为"/Users/"
File file = new File(path);
allfile(file);
}

public static void allfile(File file) {
File[] files = file.listFiles();
if(files!=null && files.length>0) {
for(File temp:files) {
//如果文件是一个目录时则递归
if(temp.isDirectory()) {
allfile(temp);
}else {
System.out.println(temp);
}
}
}
}
}

2022年10月07日Java上机
https://www.eldpepar.com/coding/15955/
作者
EldPepar
发布于
2022年10月5日
许可协议