2015-10-19 39 views
0

我遇到了題目所暗示的問題。我需要標記字符串,確定操作以解決它,將數字轉換爲int類型並返回表達式。parseInt在找到不同的運算符時使用字符串

究竟是我做錯了解析和標記化?一切似乎沒問題,直到我試圖使用stringSplit。唯一允許的庫函數是Integer.parseInt()split()。在StackOverflow上有很多事情可以幫助,但是沒有一個只使用這兩個庫函數。下面是代碼我有迄今:

public static void main(String[] args) 
{ 
    String a[] = {"12 + 34", "56 - 78", "99 * 99", "10/3"}; 
    stringSplit(a, ','); 
} 

public static int parseInt(String a) 
{ 
    int i; 
    int sum = 0; 
    double x = Integer.parseInt(a); 

    for(i = 0; i < a.length(); i++) 
     sum = sum + x; 

    System.out.printf("%s = %d\n", sum); 
} 

最終的結果應該看起來像:

12 + 34 = 46.00

56 - 78 = -22.00

等。我並不是真的在尋找答案。更多的是導致我的答案。提前感謝您的幫助!

+2

代碼'stringSplit'方法缺少.. –

+2

而作爲錯誤指示,你缺少的'parseInt' return語句。該方法應該有一個return語句,或者你應該使它成爲'void'而不是'int'。請修復這些基本錯誤並提出具體問題。你目前的問題遍佈全球,很難找到一個具體的問題。 –

+0

@AniketKulkarni謝謝!我現在正在研究,以便正確使用該方法。 – NaughtyBear

回答

2

這裏是你似乎試圖做的工作版本。我將輸入數組的每個元素分割在空間上(" "),然後提取出兩個操作數和一個運算符。我還將支票除以零。

public static void main(String[] args) throws Exception { 
    String a[] = {"12 + 34", "56 - 78", "99 * 99", "10/3"}; 
    stringProcess(a); 
} 

public static void stringProcess(String[] a) { 
    for (int i=0; i < a.length; ++i) { 
     String[] parts = a[i].split(" "); 
     double operand1 = Double.parseDouble(parts[0]); 
     String operator = parts[1]; 
     double operand2 = Double.parseDouble(parts[2]); 
     double result = 0.0; 

     switch (operator) { 
      case "+": 
       result = operand1 + operand2; 
       break; 

      case "-": 
       result = operand1 - operand2; 
       break; 

      case "*": 
       result = operand1 * operand2; 
       break; 

      case "/": 
       if (operand2 == 0) { 
        throw new IllegalArgumentException("Divide by zero!"); 
       } 
       result = operand1/operand2; 
       break; 
     } 

     System.out.println(operand1 + " " + operator + " " + operand2 + 
      " = " + String.format("%.2f", result)); 
    } 
} 

輸出:

12.0 + 34.0 = 46.00 
56.0 - 78.0 = -22.00 
99.0 * 99.0 = 9801.00 
10.0/3.0 = 3.33 
+0

非常好!然而,由於這是作業,我會看看我是否可以通過案例陳述找到另一種方式。謝謝你,先生! :d – NaughtyBear

1
public class Test{ 
    public static void main(String[] args) { 
     String a[] = { "12 + 34", "56 - 78", "99 * 99", "10/3" }; 
     parseInt(a); 
    } 

    public static void parseInt(String[] a) { 
     int sum = 0; 
     for (int i = 0; i < a.length; i++) { 
      String[] pieces = a[i].split(" "); 
      if("+".equals(pieces[1])){ 
       sum = Integer.valueOf(pieces[0]) + Integer.valueOf(pieces[2]); 
      }else if("-".equals(pieces[1])){ 
       sum = Integer.valueOf(pieces[0]) - Integer.valueOf(pieces[2]); 
      }else if("*".equals(pieces[1])){ 
       sum = Integer.valueOf(pieces[0]) * Integer.valueOf(pieces[2]); 
      }else { 
       sum = Integer.valueOf(pieces[0])/Integer.valueOf(pieces[2]); 
      } 
      System.out.println("sum" + sum); 

     } 
    } 
} 
1
As per method syntax. 
<Access specifier><modifier><return type><method name>(arguments). If you are giving int as return type your method should return a value, else make the return type as void if you do not need any return value.