我正在尝试在我的第一个程序Java 中制作一个简单的基于文本的计算器,但我无法弄清楚如何将输入String
转换为变量opOne
。然后我会尝试numOne
反对numTwo
使用opOne
作为操作员。代码如下:
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");
}
}
}