高级计算器。实现多个操作数

时间:2015-03-25 03:25:09

标签: java

需要一种方法来完成我的'高级'计算器。我已经用循环集思广益,但我自己也很困惑。关于如何实现数字操作数的任何想法,而不仅仅是嵌套一堆if语句和硬编码。

import java.util.*;

公共班级计算器{

public static void main(String[] args) {

    Scanner scan = new Scanner(System.in);
    System.out.println("welcome to your personal calculator. is your problem simple or advanced(multiple calculations)\n"
            + "Type 1 for simple or 2 for advanced");
    int choice = scan.nextInt();
    if(choice == 1){
        System.out.println("Simple equation");

        System.out.println("Enter the first number");
        int firstnum = scan.nextInt();
        System.out.println("Enter the second number");
        int secondnum = scan.nextInt();
        System.out.println("are you adding, subtracting multiplying or dividing? ENTER +,*,-, or /");
        String operand = scan.next();
        if(operand.equals("+")){
            int solution = firstnum + secondnum;
            System.out.println(firstnum + operand + secondnum + "=" + solution);
        }
        else if (operand.equals("*")){
            int solution = firstnum * secondnum;
            System.out.println(firstnum + operand + secondnum + "=" + solution);
        }
        else if(operand.equals("-")){
            int solution = firstnum - secondnum;
            System.out.println(firstnum + operand + secondnum + "=" + solution);
        }
        else if(operand.equals("/")){
            int solution = firstnum / secondnum;
            System.out.println(firstnum + operand + secondnum + "=" + solution);
        }
        else{
            System.out.println("Did not recognize operation type please restart program.");
            return;
        }

        }

    else if(choice == 2){
        System.out.println("Advanced equation");
        System.out.println("How many numbers are in your equation? max of 7\n"
                + "operands are chosen by typing +,-,*,or /");
        int numofnumbers = scan.nextInt();
        if(numofnumbers == 2){
            System.out.println("enter first num");
            int firstnum = scan.nextInt();
            System.out.println("enter second num");
            int secondnum = scan.nextInt();
            System.out.println("enter operand");
            String operand = scan.next();
            if(operand.equals("+")){
                int solution = firstnum + secondnum;
                System.out.println(firstnum + operand + secondnum + "=" + solution);
            }
            else if (operand.equals("*")){
                int solution = firstnum * secondnum;
                System.out.println(firstnum + operand + secondnum + "=" + solution);
            }
            else if(operand.equals("-")){
                int solution = firstnum - secondnum;
                System.out.println(firstnum + operand + secondnum + "=" + solution);
            }
            else if(operand.equals("/")){
                int solution = firstnum / secondnum;
                System.out.println(firstnum + operand + secondnum + "=" + solution);
            }
            else{
                System.out.println("Did not recognize operation type please restart program.");
                return;
            }

        }
        else if(numofnumbers == 3){
            System.out.println("enter first num");
            int firstnum = scan.nextInt();
            System.out.println("enter second num");
            int secondnum = scan.nextInt();
            System.out.println("enter thir num");
            int thirdnum = scan.nextInt();
        }
        else if(numofnumbers == 4){
            System.out.println("enter first num");
            int firstnum = scan.nextInt();
            System.out.println("enter second num");
            int secondnum = scan.nextInt();
            System.out.println("enter third num");
            int thirdnum = scan.nextInt();
            System.out.println("enter fourth num");
            int fourthnum = scan.nextInt();
        }
        else if(numofnumbers == 5){
            System.out.println("enter first num");
            int firstnum = scan.nextInt();
            System.out.println("enter second num");
            int secondnum = scan.nextInt();
            System.out.println("enter third num");
            int thirdnum = scan.nextInt();
            System.out.println("enter fourth num");
            int fourthnum = scan.nextInt();
            System.out.println("enter fifth num");
            int fifthnum = scan.nextInt();
        }
        else if(numofnumbers == 6){
            System.out.println("enter first num");
            int firstnum = scan.nextInt();
            System.out.println("enter second num");
            int secondnum = scan.nextInt();
            System.out.println("enter third num");
            int thirdnum = scan.nextInt();
            System.out.println("enter fourth num");
            int fourthnum = scan.nextInt();
            System.out.println("enter fifth num");
            int fifthnum = scan.nextInt();
            System.out.println("enter sixth num");
            int sixthnum = scan.nextInt();
        }
        else if(numofnumbers == 7){
            System.out.println("enter first num");
            int firstnum = scan.nextInt();
            System.out.println("enter second num");
            int secondnum = scan.nextInt();
            System.out.println("enter third num");
            int thirdnum = scan.nextInt();
            System.out.println("enter fourth num");
            int fourthnum = scan.nextInt();
            System.out.println("enter fifth num");
            int fifthnum = scan.nextInt();
            System.out.println("enter sixth num");
            int sixthnum = scan.nextInt();
            System.out.println("enter seventh num");
            int seventhnum = scan.nextInt();
        }
    }
    else{
        System.out.println("you did not type one or two. please restart program.");
        return;
    }

}     }

1 个答案:

答案 0 :(得分:1)

我喜欢这个挑战,因为它首先是计算机的核心所在,计算方程式!让我首先考虑的是计算机本身通常如何执行这些操作。据我所知,计算机一次完成一个操作(你得到了那个部分!)然后存储结果并基本上重新评估剩余的问题,好像该操作已经完成。

所以4 + 3 + 2

简单地成为

7 + 2

等等

为了在java中执行此类操作,我认为最好熟悉一些概念,例如ArrayList和String.split。

你需要ArrayList来解决问题,而不是更传统的数组,因为问题的长度会在整个程序中发生变化,正如我们在上面看到的4 + 3 + 2示例。因此,如果您尝试使用for循环遍历数组,但数组需要不断缩短,这可能会变得非常复杂。

ArrayList是一个很棒的对象,它为我们提供了一个动态的形式"数组有各种有用的方法。

您可以像这样声明一个ArrayList。

ArrayList<*variable type*> *Name* = new ArrayList<*same variable type*>();

然后你可以使用一堆不同的方法,如.add将一个项目添加到列表的末尾(将长度增加1),.删除一个项目(及其包含的索引减少长度为1)和.get(idx)替换了Array [idx]。

现在,您希望将操作符存储在与操作数相同的对象中,因为您希望保持其顺序。为了做到这一点,你最好的办法是将整个条目存储为一个字符串,就像一个句子一样,然后你可以将整个条目分开。这就是String.split的用武之地。

您可以查看有关其使用的这个很棒的条目:

https://stackoverflow.com/a/13525053/4710112

使用此:

String.split("(?<=[-+*/])|(?=[-+*/])");

您可以按顺序返回一个包含其运算符旁边每个数字的数组。

所以4 + 3 + 2

变为

4,+,3,+,2

现在这非常重要!!

您不能将从拆分返回的数组直接放入arraylist中! 你必须先将它存储在一个传统的阵列中,然后你可以'#34; feed&#34;使用for循环和ArrayList.add()将其放入ArrayList中。

现在我不打算为你解决这个问题,因为我确定你会想要自己解决这个问题。但随时都可以打我!

哦,最后几件事。您将需要.parseToDouble将ArrayList中的字符串转换回双精度数据,并将.toString转换回字符串并将它们放回ArrayList中。

哦,你应该学会编写和调用方法,在组织方面让你的生活更轻松。

例如在main而不是:

double result;
double a;
double b;
result = a+b

你可以这样做:

result = add(a,b);

只要您在main下面编写方法:

public double add(double x, double y){

double tempResult;
tempResult = x + y;
return tempResult;
}

最初可能看起来更像打字,但是当你习惯于编写类似的方法时,你会发现你的整体代码越来越短