不太完善,你再加点输入的逻辑判断什么的,这个演示只能输入正确的数据才能运行,否则会报错,仅供参考。(求采纳啊)
public class C {
static Double rs;
public static Double yunsuan(String r,Double a1, Double a2){
Double result;
if(r.equals("+")){
result=a1+a2;
System.out.println("运算结果为:"+result);
}else if(r.equals("-")){
result=a1-a2;
System.out.println("运算结果为:"+result);
}else if(r.equals("*")){
result=a1*a2;
System.out.println("运算结果为:"+result);
}else if(r.equals("/")){
result=a1/a2;
System.out.println("运算结果为:"+result);
}else{
result=a1*a2;
System.out.println("运算结果为:"+result);
}
return result;
}
public static void main(String[] args) {
int ss=0;
while(ss==0){
Scanner scan=new Scanner(System.in);
System.out.println("请输入第一个数:");
Double a1=scan.nextDouble();
System.out.println("请输入运算符(+、-、*、/、%):");
String r=scan.next();
System.out.println("请输入第二个数:");
Double a2=scan.nextDouble();
rs=yunsuan(r,a1,a2);
System.out.println("请输入指令(继续运算/新的运算/结束运算)");
String ff=scan.next();
if(ff.equals("继续运算")){
System.out.println("请输入运算符(+、-、*、/、%):");
r=scan.next();
System.out.println("请输入第二个数:");
Double a3=scan.nextDouble();
rs=yunsuan(r,rs,a3);
System.out.println("请输入指令(继续运算/新的运算/结束运算)");
ff=scan.next();
}else if(ff.equals("新的运算")){
continue;
}else{
ss=1;
}
}
}
}