4

我正在尝试在我的第一个程序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");

       } 
   }
}
4

3 回答 3

6

最简单的方法是使用一系列if-then-else语句:

if ("+".equals(opOne)) {
    res = numOne + numTwo;
} else if ("-".equals(opOne)) {
    res = numOne - numTwo;
} ...

一种高级方法是为您的操作员定义一个接口,并将实例放入Map容器中:

interface Operation {
    int calculate(int a, int b);
}

static final Map<String,Operation> opByName = new HashMap<String,Operation>();
static {
    opByName.put("+", new Operation() {
        public int calculate(int a, int b) {
            return a+b;
        }
    });
    opByName.put("-", new Operation() {
        public int calculate(int a, int b) {
            return a-b;
        }
    });
    opByName.put("*", new Operation() {
        public int calculate(int a, int b) {
            return a*b;
        }
    });
    opByName.put("/", new Operation() {
        public int calculate(int a, int b) {
            return a/b;
        }
    });
}

使用这样初始化的地图,您可以执行如下计算:

int res = opByName.get(opOne).calculate(numOne, numTwo);
于 2013-03-02T01:17:00.713 回答
1

计算机本身不知道operator(人类语言)是什么,因此您需要告诉计算机这样做。

String opOne;

try {
    opOne = br.readLine();
} catch (IOException ioe) {
    System.out.println("error");
    System.exit(1);
}

switch (opOne) {  //Note: String in switch is possible only from Java 7, check your version
     "+": System.out.println('Result: ' + (numOne + numTwo)); break; 
     "-": System.out.println('Result: ' + (numOne - numTwo)); break;
     // and so on...
}
于 2013-03-02T01:15:19.667 回答
1

您无法将 Java 字符串转换为运算符。它不是 Java 数据类型。

   double Calculate(String opOne, double numOne, double numTwo) {  
      if (opOne.equals("+"))  
        return numOne + numTwo;  
      else if (opOne.equals("-"))  
        return numOne - numTwo;  
      else if (opOne.equals("*"))  
        return numOne * numTwo; 
      else if (opOne.equals("/"))  
        return numOne / numTwo;   
    } 
于 2013-03-02T01:26:46.397 回答