简体   繁体   English

在Java中使用Random和运算符创建方程式

[英]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"; 字符串公式=“ 5 + 3”;

then another method will solve it. 那么另一种方法将解决它。

I don't really know how to create the string. 我真的不知道如何创建字符串。 Should i use concat? 我应该使用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. 现在,我只打印应该处理括号的“ P”。

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. 这可能导致7个以上的运算符。 You should probably add logic to either treat a pair of parenthesis as 1 operator or 1 parenthesis as a single operator. 您可能应该添加逻辑以将一对括号视为1个运算符或将1个括号视为单个运算符。 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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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