2011-03-26 72 views
0

我想知道如何迭代一个字符串,这是一个计算像“34 + 35 =” 它遍历每个字符,如果字符是一个数字,下一个字符也是一个数字等,它将它添加到字符串中。计算器输入是字符串

import java.io.*; 

public class SimpleTest{ 

    static double operand1; 
    static char operator; 
    static boolean isnum; 
    static double result; 
    static String calc = "45+45="; 
    static String in = ""; 
    public static void main(String[] argv){ 

    SimpleCalculator calculator = new SimpleCalculator(); 


     while (calculator.c != '=') 
     { 
      in = calculator.input(calc,operand1); 
      calculator.parsevalue(operand1, in); 
     } 


     System.out.println(calculator.getResult()); 
     } 

} 

public String input(String calc, double operand1) 
{ 
String in = ""; 
//66 
//++2 
     for (int i = 0; i < calc.length(); i++) 
     { 
     c = calc.charAt(i); 
     String characterString = Character.toString(c); 
     if (Character.isDigit(calc.charAt(i))) 
     { 
     while (Character.isDigit(calc.charAt(i+1))) 
     { 
       characterString = characterString + Character.toString(calc.charAt(i+1)); 
       i++; 
     } 

     in = characterString; 
     } 
     parsevalue(operand1,in); 

     } 
return in; 
} 

    public void parsevalue(double operand1, String in) 
    { 

String characterString = in; 
      try 
      { 

       setOperand(Double.parseDouble(characterString)); 

        if (flag == 0) 

        { 

         operand1 = (Double.parseDouble(characterString)); 
         result = operand1; 
         flag = 1; 
        } 

        getResult(); 

       } 

      catch (Exception e) 
      { 
       try 
       { 
        operator = c; 
       } 
       catch (Exception e2) 
       { 
        System.out.println("Enter a number"); 
       } 
      } 
     } 

    public void setOperator(char operator){ 
    this.operator = operator; 
    } 

    public char getOperator(){ 
    return operator; 
    } 

    public void setOperand(double operand){ 

     this.operand = operand; 
    } 

    public double getOperand(){ 

     return operand; 
    } 

    public double getResult(){ 

     if (getOperator() == '+') 
      { 
      result = (result + getOperand()); 
      } 
      if (getOperator() == '-') 
      { 
      result = (result - getOperand()); 
      } 
      if (getOperator() == '*') 
      { 
      result = (result * getOperand()); 
      } 
      if (getOperator() == '/') 
      { 
      if (getOperand() == 0) 
      { 
       System.out.println("Cannot divide by zero"); 
      } 
      result = (result/getOperand()); 
      } 

      return result; 
    } 
+6

说真的,你想让我们为你做作业吗?你不会学到任何东西 – cusimar9 2011-03-26 18:44:47

+0

是的,就像cusimar9说的,除非你尝试一些东西,否则你不会学习。告诉我们你迄今为止所做的一切。也许从那里,我们可以帮助你。 – asgs 2011-03-26 18:46:37

+0

@spookyjon:他(或她)总是问几乎同样的问题并不真正有帮助。 – 2011-03-26 18:58:21

回答

1

我会建议熟悉使用可用精细的Javadoc从Oracle:String class Javadoc

尤其是,您可能会发现charAt()方法有用。

2

如果它始终是格式XXX + YYY =那么您应该使用类String中的方法。 replace摆脱'=',split得到两个数字,并将它们解析为整数,然后计算值。

0

超越StringInteger类中的功能,可以帮助你,我建议你读入Polish notationReverse Polish notation以及它们如何被用来做计算。您的作业可能不是必需的,但理解它们以及它们与您所做的事情之间的关系仍然是一个好主意。