贴代码
本例测试是基于junit ,eclipse可安装对应 的java包
我用的是idea,添加插件即可
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
public class Calculator {
public double add(String a, String b) {
double c=0;
try {
Integer.parseInt(a);
Integer.parseInt(b);
c=Integer.parseInt(a)+Integer.parseInt(b);
System.out.println("正在计算");
} catch (NumberFormatException e) {
e.printStackTrace();
}finally {
System.out.println("请输入正确的数字");
}
return c;
}
public double substract(String a, String b) {
double c=0;
try {
Integer.parseInt(a);
Integer.parseInt(b);
c=Integer.parseInt(a)-Integer.parseInt(b);
System.out.println("正在计算");
} catch (NumberFormatException e) {
e.printStackTrace();
}finally {
System.out.println("请输入正确的数字");
}
return c;
}
public double multiply(String a, String b) {
double c=0;
try {
Integer.parseInt(a);
Integer.parseInt(b);
c=Integer.parseInt(a)*Integer.parseInt(b);
System.out.println("正在计算");
} catch (NumberFormatException e) {
e.printStackTrace();
}finally {
System.out.println("请输入正确的数字");
}
return c;
}
public double divide(String a, String b) {
double c=0;
try {
Integer.parseInt(a);
Integer.parseInt(b);
c=Integer.parseInt(a)/(double)Integer.parseInt(b);
System.out.println("正在计算");
} catch (NumberFormatException e) {
e.printStackTrace();
}finally {
System.out.println("请输入正确的数字");
}
return c;
}
public float mixOpt(String s) throws Exception {
if((s.equals("")||s.equals(null)))
throw new Exception("请输入正确的表达式");
if (s.endsWith("+")||s.endsWith("-")||s.endsWith("*")||s.endsWith("/")){
throw new Exception("请输入正确的表达式");
}
if (s == null || "".equals(s.trim())) { //trim用于删除字符串左右两边的空格
return 0f; //方法的返回类型为float,0f代表单精度的0.
}
//indexOf 方法返回一个整数值,指出 String 对象内子字符串的开始位置。如果没有找到子字符串,则返回-1。
int a1 = s.indexOf("+");
int a2 = s.indexOf("-");
int a3 = s.indexOf("*");
int a4 = s.indexOf("/");
int a5 = s.indexOf("(");
if (a1 == -1 && a2 == -1 && a3 == -1 && a4 == -1) {
if (s.trim() == null || "".equals(s.trim())) {
throw new Exception("operate error");
}
return Float.parseFloat(s.trim());
}
if (a5 != -1) {
int a6 = s.indexOf(")");
if (a6 == -1) {
throw new Exception("括号不匹配");
} else {
float f = mixOpt(s.substring(a5 + 1, a6).trim());
s = s.replace(s.substring(a5, a6 + 1), String.valueOf(f)); //使用第二个参数替换第一个参数
//String.valueOf(float f) : 将 float 变量 f 转换成字符串
return mixOpt(s);
}
}
if (a1 != -1) {
return mixOpt(s.substring(0, a1)) + mixOpt(s.substring(a1 + 1, s.length()));
}
if (a2 != -1) {
return mixOpt(s.substring(0, a2)) - mixOpt(s.substring(a2 + 1, s.length()));
}
if (a3 != -1) {
return mixOpt(s.substring(0, a3)) * mixOpt(s.substring(a3 + 1, s.length()));
}
if (a4 != -1) {
return mixOpt(s.substring(0, a4)) / mixOpt(s.substring(a4 + 1, s.length()));
}
return Integer.parseInt(s.trim());
}
}
测试类
import org.junit.jupiter.api.Test;
import java.util.Scanner;
import static org.junit.jupiter.api.Assertions.*;
class CalculatorTest {
@Test
void add( ) {
System.out.println("请输入两个要运算的数字");
String a="9";
String b="6";
double c= new Calculator().add(a,b);
System.out.println(c);
System.out.println("新的测试用例");
a="a";
b="9";
c= new Calculator().add(a,b);
System.out.println(c);
}
@Test
void substract() {
System.out.println("请输入两个要运算的数字");
String a="9";
String b="6";
double c= new Calculator().substract(a,b);
System.out.println(c);
System.out.println("新的测试用例");
a="a";
b="9";
c= new Calculator().add(a,b);
System.out.println(c);
}
@Test
void multiply() {
System.out.println("请输入两个要运算的数字");
String a="9";
String b="6";
double c= new Calculator().multiply(a,b);
System.out.println(c);
System.out.println("新的测试用例");
a="a";
b="9";
c= new Calculator().add(a,b);
System.out.println(c);
}
@Test
void divide() {
System.out.println("请输入两个要运算的数字");
String a="9";
String b="6";
double c= new Calculator().divide(a,b);
System.out.println(c);
System.out.println("新的测试用例");
a="a";
b="9";
c= new Calculator().add(a,b);
System.out.println(c);
}
@Test
void mixOpt() throws Exception {
String s="6+7-4*2";
double c=new Calculator().mixOpt(s);
System.out.println(c);
System.out.println();
s="65+";
c=new Calculator().mixOpt(s);
System.out.println(c);
}
}