简体   繁体   中英

Generating random math questions

We have to make a program than generates math problems for different year levels, so they vary with difficulty. We have to produce 2 random numbers within a min and max boundary. We then have to make these two numbers carry out an operation. For example add them together, or divide one by the other or multiply them etc.

The program works based on the year level the user supplies. Each year level has different minimum and maximum numbers, and different operations. Eg. Year 1 is only addition and subtraction and year 7 is addition subtraction multiplication division and so on. I've defined different methods to help me do this, but I can't seem to get my program to generate what I want. It should look something like the image attached below. Part of my code is here. When I run my program it just produces a number of integers (10 or 20 depending on how many math questions the user selected to attempt). It doesn't produce any of the operations between the numbers eg( +, -, /, x)

Can anyone point me in the right direction as to what I'm doing wrong?

private static void generateQuestion(int yearLevel) {
    int min = getMin(yearLevel);
    int max = getMax(yearLevel);

    int num1 = (int) (min + (max - min) * Math.random());
    int num2 = (int) (min + (max - min) * Math.random());

    int oper = getOper(yearLevel);
    String result = " ";
    char op;
    switch (oper) {
        case 1:
            op = '+';
            result = (num1 + num2 + " ");
            break;
        case 2:
            op = '-';
            result = (num1 - num2 + " ");
            break;
        case 3:
            op = '*';
            result = (num1 * num2 + " ");
            break;
        case 4:
            op = '/';
            result = (num1 / num2 + " ");
            break;
        case 5:
            op = '%';
            result = (num1 % num2 + " ");
            break;

    }
    ;
}

private static int getMin(int yearLevel) {
    int min = 0;
    if (yearLevel == 0 || yearLevel == 1 || yearLevel == 2 || yearLevel == 3 || yearLevel == 4) {
        min = 0;
    }
    if (yearLevel == 5 || yearLevel == 6) {
        min = -999;
    }
    if (yearLevel == 7) {
        min = -9999;
    }

    return min;
}

private static int getMax(int yearLevel) {

    int max = 9;
    if (yearLevel == 0 || yearLevel == 1 || yearLevel == 2 || yearLevel == 3 || yearLevel == 4) {
        max = 9;
    }

    if (yearLevel == 5 || yearLevel == 6) {
        max = 999;
    }
    if (yearLevel == 7) {
        max = 9999;
    }

    return max;

}

public static int getOper(int yearLevel) {

    yearLevel = 0;
    int opBounds = 1;
    if (yearLevel == 1 || yearLevel == 2) {
        opBounds = 2;
    }
    if (yearLevel == 3 || yearLevel == 4 || yearLevel == 5) {
        opBounds = 4;
    }

    if (yearLevel == 7) {
        opBounds = 5;
    }

    return opBounds;

}

}

I'm quite a beginner like you and I would make additional class Question to hold generated riddle and correct result. Then in your

private static Question generateQuestion(int yearLevel) {
    Question out=new Question();
    ...
    //build string for selected case, for example
    String answer=num1+" / "+num2;
    double result=num1/num2; //rather use double

    out.answer=answer; //String field
    out.result=result;

    return out;
}

And then proces it by showing the question and comparing it to expected result.

you've declared result as a String and tried to add numbers in it, you can't do that.
So this code:

String result = " ";
    char op;
    switch (oper) {
        case 1:
            op = '+';
            result = (num1 + num2 + " ");
            break;
        case 2:
            op = '-';
            result = (num1 - num2 + " ");
            break;

Change to something like:

int result = 0;
    char op;
    switch (oper) {
        case 1:
            op = '+';
            result = (num1 + num2);
            break;
        case 2:
            op = '-';
            result = (num1 - num2);
            break;

Then you'll have to use java methods like "Integer.toString(int i)" to print out the integer results (example).

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