我想在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");
}
}
}
* 「基於文本計算器」 *使用['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