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

参见英文答案 >

Is it possible to pass arithmetic operators to a method in java?                                    8个

我试图在java中创建一个简单的基于文本的计算器,我的第一个程序,EVER,我无法弄清楚如何将输入字符串转换为变量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");

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值