2013-03-02 68 views
2

我想在java中編寫一個簡單的基於文本的計算器我的第一個程序,EVER我不知道如何將輸入String變成變量opOne。然後,我將嘗試使用opOne作爲運營商,針對numTwo運行numOne。 代碼如下:如何在Java中將字符串轉換爲運算符?

import java.io.*; 
import java.math.*; 

public class ReadString { 

    public static void main (String[] args) { 


     System.out.print("Enter the first number: "); 


     BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
     int numOne = 0 ; 
     int numTwo = 0 ; 
     String opOne = null; 

     while(true){ 
     try { 
      numOne = Integer.valueOf(br.readLine()); 
      break; 
     } catch (IOException error) { 
     System.out.println("error; try again."); 
     System.exit(1); 
     } 
     catch (NumberFormatException nfe) { 
      System.out.println("error;try again."); 

     } 

     } 

     System.out.print("Enter the second number: "); 

     while(true){ 
     try { 

      numTwo = Integer.valueOf(br.readLine()); 
      break; 
     } catch (IOException error2) { 
      System.out.println("error"); 
      System.exit(1); 
     } catch (NumberFormatException nfe) { 
      System.out.println("error;try again."); 

     } 
     } 

     System.out.println("What would you like to do with " + numOne + " and " + numTwo + "?"); 

     try { 
      operator = br.readLine(); 
     } catch (IOException ioe) { 
      System.out.println("error"); 
      System.exit(1); 
     } catch (NumberFormatException nfe) { 
      System.out.println("error"); 

     } 
    } 
} 
+0

* 「基於文本計算器」 *使用['ScriptEngine'(http://docs.oracle.com/javase/7/docs/api/javax/script/ScriptEngine.html)來代替。例如。 1)[在網頁(小程序)](http://stackoverflow.com/a/12023586/418556)2)[非GUI](http://stackoverflow.com/a/8162055/418556)3)[簡單GUI](http://stackoverflow.com/a/6426684/418556)4)[很好的GUI](http://stackoverflow.com/a/7441804/418556).. – 2013-03-02 01:21:23

回答

1

電腦,其本身不知道的operator(人類語言)是什麼,所以你需要告訴計算機做。

String opOne; 

try { 
    opOne = br.readLine(); 
} catch (IOException ioe) { 
    System.out.println("error"); 
    System.exit(1); 
} 

switch (opOne) { //Note: String in switch is possible only from Java 7, check your version 
    "+": System.out.println('Result: ' + (numOne + numTwo)); break; 
    "-": System.out.println('Result: ' + (numOne - numTwo)); break; 
    // and so on... 
} 
+0

我不能投票給你,因爲我是新的,但我會說這是一個偉大的「謝謝」 – 2013-03-02 01:17:03

+0

我沒有看到任何運行時教學 – 2013-03-02 01:18:30

4

這樣做將是if-then-else語句序列的最簡單的方法:

if ("+".equals(opOne)) { 
    res = numOne + numTwo; 
} else if ("-".equals(opOne)) { 
    res = numOne - numTwo; 
} ... 

一種先進的方法是定義一個接口爲您的運營商,並把情況在Map容器:

interface Operation { 
    int calculate(int a, int b); 
} 

static final Map<String,Operation> opByName = new HashMap<String,Operation>(); 
static { 
    opByName.put("+", new Operation() { 
     public int calculate(int a, int b) { 
      return a+b; 
     } 
    }); 
    opByName.put("-", new Operation() { 
     public int calculate(int a, int b) { 
      return a-b; 
     } 
    }); 
    opByName.put("*", new Operation() { 
     public int calculate(int a, int b) { 
      return a*b; 
     } 
    }); 
    opByName.put("/", new Operation() { 
     public int calculate(int a, int b) { 
      return a/b; 
     } 
    }); 
} 

有了這樣初始化的地圖,你可以按照如下進行計算:

int res = opByName.get(opOne).calculate(numOne, numTwo); 
1

您無法將Java字符串轉換爲運算符。這不是Java數據類型。

double Calculate(String opOne, double numOne, double numTwo) { 
     if (opOne.equals("+")) 
     return numOne + numTwo; 
     else if (opOne.equals("-")) 
     return numOne - numTwo; 
     else if (opOne.equals("*")) 
     return numOne * numTwo; 
     else if (opOne.equals("/")) 
     return numOne/numTwo; 
    } 
相關問題