在线等:写一个java运算的简单程序(我是菜鸟)

来源:百度知道 编辑:UC知道 时间:2024/09/26 13:28:09
a=b operator c
其中b和c均为常数
operator可以为加号(+),减号(-),乘号(*),除号(/)
最后显示a的名称和计算结果

用键盘输入两个数后进行+、-、*、/运算,并要考虑异常状况(如:不符合语法规范;输入数字过大,导致溢出等现象)
但是只要考虑上面的两个异常就好了

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TestDD {
public static void main(String[] args) {
try{
String sx = args[0].trim();
String sy = args[1].trim();
Pattern pattern = Pattern.compile("[0-9]+");
Matcher match1 = pattern.matcher(sx);
Matcher match2 = pattern.matcher(sy);
boolean bx = sx.startsWith("0");
boolean by = sy.startsWith("0");
double dx = Double.parseDouble(sx);
double dy = Double.parseDouble(sy);

if(args.length == 0)
throw new Exception("你没有输入参与运算的数字。。。");
else if(args.length == 1)
throw new Exception("你只输入了一个数字,不符合运算规则。。。");
else if(args.length > 2)
throw new Exception("你输入的参与运算的数字大于2个,不符合运算规则。。。");
if(dy == 0)
throw new Exception("除数为0,不符合运算规则。。");
if(bx && by)
thro