命令行计算器,Java

时间:2014-06-18 13:39:24

标签: java

来自Java命令行的输入:" 4 + 6 + 5 - 5"。 通缉结果:"是10"。 实际结果:"是5"。

class Calculator
{
    int v_in1, v_in2, v_in3, v_in4, v_answer, result;
    String v_sign1, v_sign2, v_sign3;

    public Calculator()
    {
    }



    public void count(String[] args)
    {
        for(int i = 0; i < args.length; i++)
        {
            //System.out.print(args[i]+ " ");

            if(i == 0 || i % 2 == 0)
            {   
                v_in1 = Integer.parseInt(args[i]);
                //System.out.print(v_in1 + " ");
            }

            switch(args[i])
            {
                case "+": {
                    v_answer += v_in1;
                    break;
                }
                case "-": {
                    v_answer -= v_in1;
                    break;
                }
            }
        }

        System.out.print("is " + v_answer);
    }

}

可能存在一些额外的问题例如声明的变量太多等等,但我关注的是for-if-switch部分,我无法固定 - 指出问题。

谢谢:)

5 个答案:

答案 0 :(得分:2)

问题在于您是将操作应用于之前的号码,而不是应用于下一个号码。相反,您应该记住操作员并在看到数字时更新结果,例如像这样:

int sign = +1, result = 0;
for (String arg : args) {
    switch (arg) {
        case "+":
            sign = +1;
            break;
        case "-":
            sign = -1;
            break;
        default:
            result += sign * Integer.parseInt(arg);
    }
}

答案 1 :(得分:0)

这种情况正在发生,因为你的最后一个整数不会被考虑在内,因为它只是存储在v_in1中,但由于没有+, - 所以它不会被添加或减去。试试这个:

for(int i = 0; i < args.length; i++)
    {
        //System.out.print(args[i]+ " ");

        if(i == 0 || i % 2 == 0)
        {   
            v_in1 = Integer.parseInt(args[i]);
            //System.out.print(v_in1 + " ");


        }


        switch(args[i])
        {
            case "+":
            {
                v_answer = v_in1 + Integer.parseInt(args[i+1]) + v_answer;
                i++;   

            break;
            }
            case "-":
            {

                v_answer = v_in1 - Integer.parseInt(args[i+1]) + v_answer;
                i++;
            break;
            }

        }
    }

答案 2 :(得分:0)

我不知道此任务的所有要求,但我建议转换whole expression to postfix notation,然后使用堆栈将其解析为evaluate the result

答案 3 :(得分:0)

我看了一下你的方法,我建议你简化你的代码(类似于long count(String[] args)

long result = 0; // Note, this shadows your Object's result. I'm not sure why you
                 // had hard-coded fields like that. I don't think you need them.
                 // Also, this returns a long.
boolean negative = false;
for (String arg : args) {
  String oper = arg.trim();
  if (oper.equals("+")) { // Is it a plus sign?
    negative = false;
  } else if (oper.equals("-")) { // Is it a minus sign?
    negative = !negative; // - a negative value is addition
  } else {
    try {
      int i = Integer.parseInt(oper); // Parse the integer
      if (negative) {
        i = -i;
        negative = false;
      }
      result += i; // add the result
    } catch (NumberFormatException nfe) {
      System.err.println(oper + " is not +, - or a number");
    }
  }
}
return result;

答案 4 :(得分:0)

这就是练习中的作用。

class Calculator
{

int v_answer = 0;
int v_in1 = 0;


public Calculator()
{

}

public void count(String args[])
{

    System.out.println();
    System.out.print("Result of the calculation ");

    for(int i = 0; i < args.length; i++)
    {
        System.out.print(args[i]+ " ");

        if(i % 2 == 0)
        {   
            v_in1 = Integer.parseInt(args[i]); 
        }

        if(i == 0)
        {
            v_answer += Integer.parseInt(args[0]);
        }


        if(args[i].equals("+"))
        {   

            v_answer += Integer.parseInt(args[i+1]); 

        }
        else if(args[i].equals("-"))
        {
            v_answer  -=  Integer.parseInt(args[i+1]);

        }


    }

    System.out.print("is " + v_answer);
    System.out.println();
}

}