【问题标题】:Java Command Prompt Calculator - HomeworkJava 命令提示符计算器 - 作业
【发布时间】:2013-10-29 01:58:13
【问题描述】:

我需要一个在命令提示符下充当计算器的 Java 程序。我可以想出这个,但是我必须一次输入一个问题(输入“2”,按回车,输入“+”,按回车,输入“2”,按回车),我想知道我是否可以在我可以放入“2+2”的地方代替。提前感谢您的帮助!

import java.util.*;

public class memCalc
{
    public static void main (String args[])
    {
        Scanner input = new Scanner(System.in);
        String op;
        int numberOne, numberTwo, result = 0;
        numberOne = input.nextInt();
        op = input.next();
        numberTwo = input.nextInt();
        if (op.equals("+"))
        {
            result = numberOne + numberTwo;
            System.out.print("The answer is: " + result + " .\n");
        }
        else if (op.equals("-"))
        {
            result = numberOne - numberTwo;
            System.out.print("The answer is: " + result + " .\n");
        }
        else if (op.equals("*")) 
        {
            result = numberOne * numberTwo;
            System.out.print("The answer is: " + result + " .\n");
        }
        else if (op.equals("/"))
        {
            result = numberOne / numberTwo;
            System.out.print("The answer is: " + result + " .\n");
        }
    }
}

【问题讨论】:

  • 我认为这个问题不一定值得投反对票,只是需要更多解释。您已经说明了您要实现的目标,并且您发布了一些我假设您想要修改的代码以使其匹配。话虽如此,您至少尝试过什么吗?这里真正需要的只是一些简单的解析。
  • @JoshM 我真的不知道该尝试什么。即使有人告诉我我需要研究什么也会有所帮助:)
  • 尝试一些对你有意义的事情。做一些实验。提示用户输入表达式,然后使用空格分隔符调用Scanner#nextLine()String#split(String)split[0] 将是第一个数字,split[1] 将是操作,split[2] 将是第二个数字。
  • @JoshM 谢谢!我会解决这个问题,然后我们会看看我是否回来!
  • 如果您想一次获取一个行字符串,请尝试 input.nextLine()。您最好查看 Scanner 的 API 文档,了解 Scanner 支持使用 next* 方法读取什么样的值,例如 nextInt()、nextLine() 等。

标签: java command-prompt


【解决方案1】:

这不是最好的解决方案……但它确实有效。基本上我只是将用户输入的整个内容放入一个字符串中,然后找到运算符的位置,然后我使用操作的位置找到 2 个数字并将它们放入一个 int 中。之后,我使用子字符串将运算符放入字符串中。那么,这就是你所得到的。

这不是最好的代码...但是这会有所帮助

public static void main (String args[])
{
    Scanner input = new Scanner(System.in);
    String equation = input.nextLine();
    int opLocation = equation.indexOf("+");
    if(opLocation == -1)
    {
        opLocation = equation.indexOf("-");
    }
    if(opLocation == -1)
    {
        opLocation = equation.indexOf("*");
    }
    if(opLocation == -1)
    {
        opLocation = equation.indexOf("/");
    }
    String number = equation.substring(0,opLocation);
    int numberOne = Integer.parseInt(number);
    number = equation.substring(opLocation + 1);
    int numberTwo = Integer.parseInt(number);
    String op = equation.substring(opLocation,opLocation+1);
    int result;
    if (op.equals("+"))
    {
        result = numberOne + numberTwo;
        System.out.print("The answer is: " + result + " .\n");
    }
    else if (op.equals("-"))
    {
        result = numberOne - numberTwo;
        System.out.print("The answer is: " + result + " .\n");
    }
    else if (op.equals("*")) 
    {
        result = numberOne * numberTwo;
        System.out.print("The answer is: " + result + " .\n");
    }
    else if (op.equals("/"))
    {
        result = numberOne / numberTwo;
        System.out.print("The answer is: " + result + " .\n");
    }
}

【讨论】:

    猜你喜欢
    • 2014-04-25
    • 2011-10-31
    • 2011-10-31
    • 2012-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-23
    • 1970-01-01
    相关资源
    最近更新 更多