gpt4 book ai didi

java - 如何在 Java 中将字符串转换为运算符?

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:29:12 26 4
gpt4 key购买 nike

<分区>

我正在尝试用 java 制作一个简单的基于文本的计算器 我的第一个程序,EVER 但我不知道如何将输入 String 转换为变量 opOne。然后,我将尝试使用 opOne 作为运算符对 numTwo 操作 numOne。代码如下:

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");

}
}
}

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com