Building a math game in Java

后端 未结 3 620
南笙
南笙 2020-12-17 17:03

I am building a math game for java and I\'m stuck at this part as per the details of my assignment. The rules are simple: you have to use each number only once and only the

3条回答
  •  醉梦人生
    2020-12-17 17:49

    I would recommend you to use a tree structure to store the equation, i.e. a syntactic tree in which the root represents and operator having two children representing the operands and so on recursively. You would probably get a cleaner code doing it like that, because then you won't need to generate the combinations of operands "by hand", but you can make a code which picks every operand from a 1-dimensional char[] operands = new char[] {'+','-','*','/'} array.

    If you don't want to use a syntactic tree or think it's not necessary for your use case you can always try to find a different way to make the code to pick operands from the 1-dimensional array and store them into a different data structure. But I would especially avoid writing all the combinations as you are doing. It does not look very easy to maintain.

提交回复
热议问题