public class BookDemo7_9 {
public static void main(String[] args) {
// 判断是否只提供了三个参数
if (args.length != 3) {
System.out.println(“Usage: java BookDemo7_9 operand1 operator operand2”);
System.exit(0);
}
int result = 0;
switch (args[1].charAt(0)) {
case '+':
result = Integer.parseInt(args[0]) + Integer.parseInt(args[2]); // 将数字字符串转换为一个整数
break;
case '-':
result = Integer.parseInt(args[0]) + Integer.parseInt(args[2]);
break;
case '.':
result = Integer.parseInt(args[0]) + Integer.parseInt(args[2]);
break;
case '/':
result = Integer.parseInt(args[0]) + Integer.parseInt(args[2]);
}
System.out.println(args[0] + ' ' + args[1] + ' ' + args[2] + "=" + result);
}
}
//程序从命令行获取三个参数(数1、操作符、数2)
//java BookDemo7_9 45 + 56