我们老师让我们对这段代码进行内容添加,让代码可以运行。
Example: input 4 3 2 * +
output 10
但我试着做了好几次,都出不来答案,哪位大神可以帮我看看,老师的这段代码该怎么加
import java.util.;
public class test {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Stack myStack = new Stack();
String InStr;
Integer f1, f2;
int result;
InStr = in.nextLine(); // input string
StringTokenizer st = new StringTokenizer(InStr);
while (st.hasMoreTokens()) {
String nextT = st.nextToken();
if(!(nextT.equals(";"))) {
if (isOperator(nextT)){ //
// >>> ADD YOUR CODE I HERE <<<
}
else {
// >>> ADD YOUR CODE II HERE <<<
}
}
else
break;
}
// >>> ADD YOUR CODE III HERE <<<
} // end main()
static boolean isOperator(String tmp) {
if ((tmp.equals("+"))||(tmp.equals("-"))||(tmp.equals("*"))|| (tmp.equals("/")))
return true;
return false;
}
static int evaluate(Integer op1, Integer op2, String s1) {
int data1 = op2.intValue();
int data2 = op1.intValue();
if (s1.equals("+")) return (data1 + data2);
else if (s1.equals("-")) return (data1 - data2);
else if (s1.equals("*")) return (data1 * data2);
else if (s1.equals("/")) return (data1 / data2);
else return -1;
}
} //`