展开全部
用java写的计算器的程序,e69da5e887aa3231313335323631343130323136353331333363373231主要是通过控制台输入,主要方法是使用scanner类来接收用户从键盘输入的一个算式,通过分解算式,存入两个字符串,判断中间的的符号,进行相应计算,如下代码:System.out.println("-----------------------------------");
System.out.println("请输入一个算术表达式,如:45*23");
Scanner in = new Scanner(System.in);//接收用户从键盘输入的字符
String str = in.nextLine();
StringBuffer buffer = new StringBuffer();//保存左侧的数字
StringBuffer buffer1 = new StringBuffer();//保存右侧的数字
char t = ' ';//保存运算符
for (int i = 0; i
if (str.charAt(i) == '+' || str.charAt(i) == '-'
|| str.charAt(i) == '*' || str.charAt(i) == '/') {
t = str.charAt(i);//识别是什么运算符
for (int j = i + 1; j
buffer1.append(str.charAt(j));
}
break;
} else {
buffer.append(str.charAt(i));
}
}
String c = buffer.toString();
String d = buffer1.toString();
double a = Double.parseDouble(c);
double b = Double.parseDouble(d);
double sum = 0;
if (t == '+') {
sum = a + b;
}
if (t == '-') {
sum = a - b;
}
if (t == '*') {
sum = a * b;
}
if (t == '/') {
sum = a / b;
}
System.out.println("程序运算...");
System.out.println(c+t+d+"="+sum);
System.out.print("-----------------------------------");
运行结果如下: