Java随机数学方程生成随机答案

时间:2018-02-26 19:29:48

标签: java math random java.util.scanner equation

我正在编码一个问题,它将是x(+或 - 或*)y = z,它将为用户生成4个可能的答案,其中有1个好的和3个错误的答案。我制作了大部分代码,但是我不知道如何为Reponse()再次使用相同的公式,因为现在当我执行代码时,Equation()生成自己的代码,Reponse()执行另一个不同的公式。另外我需要知道如何通过添加一个显示类似5 +5 =的公式的系统来确保代码的工作原理? 并且代码将显示4个答案,其中一个答案很好。

这里是代码:

    public class Equation {

    int x, y, z;


    public Equation() {
        Random r = new Random();
        x = r.nextInt(50) + 1;
        y = r.nextInt(50) + 1;
        z = 0;
        char operator = '?';

        switch (r.nextInt(3)) {
            case 0:
                operator = '+';
                z = x + y;
                break;
            case 1:
                operator = '-';
                z = x - y;
                ;
                break;
            case 2:
                operator = '*';
                z = x * y;
                ;
                break;
            default:
                operator = '?';
        }

        System.out.print(x);
        System.out.print(" ");
        System.out.print(operator);
        System.out.print(" ");
        System.out.print(y);
        System.out.print(" = ");
        System.out.println(z);

    }

}

和Reponse()生成答案的那个:

    public class Reponse {
    Equation equ = new Equation();
    int a, b, c, d;
    char operator = '?';

    public Reponse() {
        Random r = new Random();
        switch (r.nextInt(4)) {

            case 0:
                a = r.nextInt(2 * equ.z);
                break;

            case 1:
                b = r.nextInt(2 * equ.z);
                break;

            case 2:
                c = r.nextInt(2 * equ.z);
                break;
            case 3:
                d = equ.z;
                break;
            default:
                operator = '?';
        }
    }
}

1 个答案:

答案 0 :(得分:1)

这是因为您正在public async makeSureDirectoryExists(relDirPath: string): Promise<boolean> { console.log(`making sure rel Path: ${relDirPath}`); const absolutePath = this.file.dataDirectory + relDirPath; console.log(`making sure abs Path: ${absolutePath}`); const pathParts = relDirPath.split('/'); const doesWholePathExist: boolean = await this.doesExist(absolutePath); if (doesWholePathExist) { return true; } let currentPath = this.file.dataDirectory; while (pathParts.length > 0) { const currentDir = pathParts.shift(); const doesExist: boolean = await this.doesExist(currentPath + currentDir); if (!doesExist) { console.log(`creating: currentPath: ${currentPath} currentDir: ${currentDir}`); const dirEntry: DirectoryEntry = await this.file.createDir(currentPath, currentDir, false); if (!dirEntry) { console.error('not created!'); return false; } } currentPath = currentPath + currentDir + '/'; } return true; } 类中初始化Equation类的新实例。

Response

每当你做某事时,

Equation equ = new Equation();

将实例化Response r = new Response(); 的新实例。

您应该做的事情如下,

  1. 按如下方式更改Equation课程:

    Response
  2. 注意:我已从类中删除了public class Response { int a, b, c, d; char operator = '?'; public Response(Equation equ) { Random r = new Random(); switch (r.nextInt(4)) { case 0: a = r.nextInt(2 * equ.z); break; case 1: b = r.nextInt(2 * equ.z); break; case 2: c = r.nextInt(2 * equ.z); break; case 3: d = equ.z; break; default: operator = '?'; } } } 的实例,并将其传递给构造函数。

    1. 创建Equation

      的新实例
      Equation
    2. 通过传递Equation equ = new Equation(); 个实例

      来创建Response的新实例
      Equation
    3. 现在,您可以使用您实例化的Response r = new Response(equ); 类的相同实例创建Response类的多个实例。