2011-03-26 70 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以及它們如何被用來做計算。您的作業可能不是必需的,但理解它們以及它們與您所做的事情之間的關係仍然是一個好主意。