【问题标题】:Calculator in java [closed]java中的计算器[关闭]
【发布时间】:2013-12-15 23:01:02
【问题描述】:

最近我研究了一个可以加减乘除的简单计算器。

public static void main(String arr[]){ 
    double num1, num2;
    String ans = null;

    System.out.println("Calculator manu:");
    System.out.println("\n"+"a for adding"); 
    System.out.println("b for substracting");
    System.out.println("c for multiplying");
    System.out.println("d for dividing");

    Scanner NumInput = new Scanner(System.in);

    System.out.println("\n"+"Enter number one: ");
    num1 = NumInput.nextDouble();

    System.out.println("\n"+"Enter number two: ");
    num2 = NumInput.nextDouble();

    while(true)
    {   
    Scanner AnsInput = new Scanner(System.in);
    System.out.println("\n"+"Choose operation.");
    String answer = AnsInput.next();


    if(AnsInput.equals("1"))
    {
        answer = Double.toString(num1+num2);
        System.out.println("\n"+"The sum of the first and second number is: "+answer);
    }
    else if(AnsInput.equals("2")) 
    {
        answer = Double.toString(num1-num2);
        System.out.println("\n"+"Subtraction of the first and the second number is: "+answer);
    }
    else if(AnsInput.equals("3"))
    {
        answer = Double.toString(num1*num2);
        System.out.println("\n"+"The product of the first and second number is: "+answer);
    }
    else if(AnsInput.equals("4"))
    {
        answer = Double.toString(num1/num2);
        System.out.println("\n"+"Ratio of the first and the second number is: "+answer);
    }
    }
}

但是如果我想编写一个像普通计算器一样工作的程序怎么办?它加、减、乘、除……,但不仅是两个数字,而是多个数字。

【问题讨论】:

  • Smells like homework. 请尝试表现出更多的努力,例如解释您到目前为止所做的尝试。

标签: java calculator


【解决方案1】:

这当然可以!虽然它需要大量的框架。但是,如果您想使用这种类型的简单代码,您需要做的是将数字存储在此函数之外的其他位置,并且每两个应用运算的数字,您将其存储并用作第一个参数您想使用的第三个。等等。这有意义吗?

【讨论】:

  • 感谢您的建议,它会派上用场的