简体   繁体   English

生成随机数学问题

[英]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. 我们必须在最小和最大边界内产生2个随机数。 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. 1年仅是加减法,而7年是加减乘除法,依此类推。 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). 当我运行程序时,它只会生成多个整数(10或20,具体取决于用户选择尝试的数学问题数)。 It doesn't produce any of the operations between the numbers eg( +, -, /, x) 它不会在数字之间产生任何运算,例如(+,-,/,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. 我是一个像您一样的初学者,我将再选一堂课Question,以容纳所产生的谜语和正确的结果。 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. 您已将result声明为String并尝试在其中添加数字,则无法这样做。
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). 然后,您将必须使用Java方法(例如“ Integer.toString(int i)”)来打印出整数结果(示例)。

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

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