看一下
谁帮我改一下,我想能做如“-5+7”的运算,然后还要能循环运算的,出来结果后能重新提示用户输入算式的:package phrase1;
import java.io.*;
public class Stringcalcul {
public static void main(String args[])throws IOException{
String teststr;
BufferedReader buf;
String A="";
String B="";
float numA,numB,result;
System.out.print("please input:");
buf=new BufferedReader(new InputStreamReader(System.in));
teststr=buf.readLine();
char[] testchar=teststr.toCharArray();
//char[] oper={'+','-','*','/'};
for(int i=0;i<testchar.length;i++){
switch(testchar[i]){
case '+':
for(int j=0;j<i;j++){
Character a=new Character(testchar[j]);
A+=a.toString();
}
for(int j=i+1;j<testchar.length;j++){
Character b=new Character(testchar[j]);
B+=b.toString();
}
numA=Float.parseFloat(A);
numB=Float.parseFloat(B);
result=numA+numB;
System.out.println(teststr+"="+result);
break;
case '-':
for(int j=0;j<i;j++){
Character a=new Character(testchar[j]);
A+=a.toString();
}
for(int j=i+1;j<testchar.length;j++){
Character b=new Character(testchar[j]);
B+=b.toString();
}
numA=Float.parseFloat(A);
numB=Float.parseFloat(B);
result=numA-numB;
System.out.println(teststr+"="+result);
break;
case '*':
for(int j=0;j<i;j++){
Character a=new Character(testchar[j]);
A+=a.toString();
}
for(int j=i+1;j<testchar.length;j++){
Character b=new Character(testchar[j]);
B+=b.toString();
}
numA=Float.parseFloat(A);
numB=Float.parseFloat(B);
result=numA*numB;
System.out.println(teststr+"="+result);
break;
case '/':
for(int j=0;j<i;j++){
Character a=new Character(testchar[j]);
A+=a.toString();
}
for(int j=i+1;j<testchar.length;j++){
Character b=new Character(testchar[j]);
B+=b.toString();
}
numA=Float.parseFloat(A);
numB=Float.parseFloat(B);
if(numB==0){
System.out.println("the second num can't be 0");
}else{
result=numA/numB;
System.out.println(teststr+"="+result);
}
break;
}
}
}
}