我想在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