是否可以在循环中增加数组索引



所以我的问题是,如果你想获得一个新的计算,我的数组中的运算器应该会改变。我该怎么做?我已经试了好几个小时了:/这就是我所走的路。所以首先它以+开始,如果你选择得到一个新的计算,下一个应该是-,以此类推。。。。所以我需要切换"操作员[0]"

private static void mathGame() {
        char[] operator = {'+', '-', '*', '/'};
        int x;
        int y = 0;
        Scanner input = new Scanner(System.in);
        do {
            int operand1 = (int) (Math.random() * (100 - 1) + 1);
            int operand2 = (int) (Math.random() * (100 - 1) + 1);
            System.out.println("Solve");
            System.out.println(operand1 + " " + operator[0] + " " + operand2);
            x = input.nextInt();
            // operator [1]++;
            if (x == evaluate(operand1, operator[0], operand2)) {
                System.out.println("True!");
                System.out.println("New Challange?");
                y = input.nextInt();
                //operator[1]++;
            } else if (x != evaluate(operand1, operator[0], operand2)) {
                System.out.println("False!");
                System.out.println("New Challange?");
                y = input.nextInt();
                //operator[1]++;
            }
        } while (y == 1);
    }

您需要在循环外定义一个变量(int i=0;),然后在循环内使用此指令(可能在"}while(y==1);"之前):

i=(i+1)%4;(4是运算符数组的大小)。

我认为你需要对你的问题提供更多的细节,并详细阐述你的问题,以便让未来的读者更清楚。

最新更新