我写了这个“计算器”,我希望它在计算后输出结果,但由于某种原因它不会!
样本输入为
2
1 + 1 * 2 =
输出为 4
提前致谢
import java.util.Scanner;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int n=input.nextInt();
for (int i=0;i<n;i++){
int result=input.nextInt();
String operand=input.next();
int flag=0;
while (flag==0){
int temp=input.nextInt();
if (operand.equals("*"))
result*=temp;
else if (operand.equals("/"))
result/=temp;
else if (operand.equals("+"))
result+=temp;
else if (operand.equals("-"))
result-=temp;
else if (operand.equals("=")){
flag=1;
System.out.println(result);
}
operand=input.next();
}
}
}
}
public class Main
{
public static void main(String[] args)
{
Scanner input=new Scanner(System.in);
int n=input.nextInt();
for (int i=0;i<n;i++)
{
int result=input.nextInt();
String operand=input.next();
int flag=0;
while (flag==0)
{
int temp=input.nextInt();
if (operand.equals("*"))
result*=temp;
else if (operand.equals("/"))
result/=temp;
else if (operand.equals("+"))
result+=temp;
else if (operand.equals("-"))
result-=temp;
else if (operand.equals("="))
{
flag=1;
System.out.println(result);
}
operand=input.next();
}
}
}
}