【问题标题】:How do you convert input "add 1 2" to "1+2" [closed]如何将输入“add 1 2”转换为“1+2”[关闭]
【发布时间】:2015-02-03 22:14:29
【问题描述】:

如果提示允许用户输入“add 1 2”

如何将其转化为 1+2 并输出答案?

“sub 1 2”也一样 到 1-2

我知道可以拆分成数组来解决这个问题,但是我的老师不会让我们为这个作业这样做,我们只允许使用 if while 和 for 循环。

我对代码的想法如下,但它不会让我通过,因为 double i = line.nextDouble() 不起作用

导入 java.util.Scanner;

公共类外壳{

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.println("Welcome to CS302Shell! "
            + "Enter help for a list of commands or exit to end.");

    String command = input.next();
    String line = input.nextLine();

    if (command.equals("add")) {
        double i = line.nextDouble();
    }






} /** method */

} /** 类 */

【问题讨论】:

  • 你去了吗?向我们展示您迄今为止的尝试,以便我们了解您需要帮助的地方。
  • 你有什么尝试吗?
  • 您能否更概括地描述您想要做什么?你是在写中缀转换器的前缀,还是更有限的东西?
  • 首先向我们展示您的代码,但最终的一种方法是使用分隔符(在本例中为空格“”)拆分字符串或输入,然后检查双方是否为 int,然后执行任何计算他们你需要。不过可能需要查看一些代码和更多解释。
  • 谷歌pocket calculator as finite state machine

标签: java string variables


【解决方案1】:

您可以使用split 方法将您的字符串拆分为正确的部分:

public static double calculate(String input) {

        // Here you splitt the input-String at every space. So you get 3 pieces
        String[] splitted = input.trim().replace(" +", " ").split(" ");

        // Set the different element-values to variables
        String operator = splitted[0];
        // Here you have to cast the String to double because you can't
        // calculate with a String
        double first;
        double second;

        //With this try-catch block you check if the numbers are really numbers.
        try {
            first = Double.valueOf(splitted[1]);
            second = Double.valueOf(splitted[2]);
        } catch (NumberFormatException ne) {
            System.err.println("Your input for one of the numbers was wrong");
            return 0;
        }
        // Now you check which operator the user wants and return the result.
        switch (operator) {
        case "add":
            return first + second;
        case "sub":
            return first - second;
        case "multiply":
            return first * second;
        case "divide":
            return first / second;

            // If de user has given a wrong input, the return is 0
        default:
            return 0;
        }
    }

    public static void main(String[] args) {
        System.out.println(calculate("add 1 2"));
    }

【讨论】:

    【解决方案2】:

    您确实应该首先发布您的尝试,以便我们可以更好地帮助您,但我认为这可能是您想要的。所有这些代码都假设您输入了正确的输入。

    public int addInput(String input){
         inputs = inputs.replace("add ", "");
         String[] inputs = input.split(" ", 2);
         return Integer.parseInt(inputs[0])+Integer.parseInt(inputs[1]);
    }
    
    public int subInput(String input){
         inputs = inputs.replace("sub ", "");
         String[] inputs = input.split(" ", 2);
         return Integer.parseInt(inputs[0])-Integer.parseInt(inputs[1]);
    }
    

    说真的,虽然看到你的代码会很好。 :)

    【讨论】:

      【解决方案3】:

      字符串解析:

      int result = 0;
      String[] data = "add 1 2".split(" ");
      switch(data[0]) {
          case "add":
              int param1 = Integer.parseInt(data[1]);
              int param2 = Integer.parseInt(data[2]);
              result = param1 + param2;
              break;
          case "sub":
              //...
              break;
      }
      

      你明白了。
      一些数据验证将是一个好主意。 还有一些在 parseInt 调用周围的 try/catch 块,以确保当有人键入“add a b”等时程序不会崩溃......

      【讨论】:

      • 好吧,显然我的老师不会让我们在这项作业中使用拆分。我们只能使用 if while for 循环和非常基本的变量声明。
      • @PeterHong - 好吧,虽然它会花费更多时间,但自己解析字符串并没有那么难。查看 String.charAt() 的 API 文档作为起点
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-30
      • 2021-06-29
      相关资源
      最近更新 更多