简体   繁体   中英

Create an equation using Random and operators in Java

I'm trying to create a method that would create an equation for me, as a String.

For example, this method would create:

String formula = "5 + 3";

then another method will solve it.

I don't really know how to create the string. Should i use concat?

Thanks for all the help.

public static String getEquation() {


    for(int i=0;i<7;i++){

    int rand = rng.nextInt(5);

    switch (rand) {

        case 0:
            operator = "+";
            break;
        case 1:
            operator = "-";
            break;
        case 2:
            operator = "*";
            break;
        case 3:
            operator = "/";
            break;
        case 4:
            operator = "(";
            break;
        case 5:
            operator = ")";
    }


}
     return formula;
}

Wrote something up really quick, I think this should get you going. You'd have to handle the cases for parentheses though. I didn't account for that.

import java.util.Random;

public class Test {

    static Random randomGenerator = new Random();
    static String operators = "+-*/P"; //p = parentheses pair

    public static char getRandomOperator() {
        return operators.charAt(randomGenerator.nextInt(operators.length()));
    }

    public static int getRandomNumber() {
        return randomGenerator.nextInt(100);
    }

    public static String createEquation() {
        //Just for proof of concept, let's do 3 operators
        String equation = "";
        int numOfOperators = 3;
        char operator = ' ';
        for (int i = 0; i < numOfOperators; i++) {
            equation += getRandomNumber();
            equation += getRandomOperator();
        }
        equation += getRandomNumber();
        return equation;
    }

    public static void main(String[] args) {
        String equation = createEquation();
        System.out.println(equation);
    }
}

I may have time later and come back and address the parentheses issue. For now I just print 'P' where parentheses should be handled.

EDIT Updated it to handle parentheses. I'm going to paste it down here in case you want to keep it as the old way. Also did a couple tweaks here and there. Note that I didn't handle it matching an exact number of operators when it comes to parenthesis. Which means that if all the open parenthesis weren't closed by the time the loop is done grabbing operators then I append extra parenthesis at the end. Which can result in more than 7 operators. You should probably add logic to either treat a pair of parenthesis as 1 operator or 1 parenthesis as a single operator. Enjoy.

import java.util.Random;

public class Test {

    static Random randomGenerator = new Random();
    static String operators = "+-*/()";
    static int opeatorStringLength = operators.length();

    public static char getRandomOperator() {
        return operators.charAt(randomGenerator.nextInt(opeatorStringLength));
    }

    public static int getRandomNumber() {
        return randomGenerator.nextInt(100);
    }

    public static String appendToEquation(String equation, String value1, String value2) {
        String temp = equation;
        temp += value1;
        temp += value2;
        return temp;
    }

    public static String createEquation(int numOfOperators) {
        String equation = "";
        char operator;
        int operand;
        int openParenCounter = 0;
        for (int i = 0; i < numOfOperators; i++) {
            operator = getRandomOperator();
            operand = getRandomNumber();
            if (operator == '(') {
                openParenCounter++;
                equation = appendToEquation(equation, Character.toString(operator), Integer.toString(operand));
            } else if (operator == ')') {
                if (openParenCounter == 0) { //Can't start off with a close parenthesis
                    openParenCounter++;
                    equation = appendToEquation(equation, "(", Integer.toString(operand));
                } else {
                    openParenCounter--;
                    equation = appendToEquation(equation, Integer.toString(operand), Character.toString(operator));
                }
            } else {
                equation = appendToEquation(equation, Integer.toString(operand), Character.toString(operator));
            }
        }
        equation += getRandomNumber();
        while (openParenCounter > 0) {
            equation += ")";
            openParenCounter--;
        }

        return equation;
    }

    public static void main(String[] args) {
        String equation;
        equation = createEquation(7); //The argument passed is the number of operators to use
        System.out.println(equation);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM