Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 是否可以在循环中增加数组索引?_Java_Arrays - Fatal编程技术网

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

Java 是否可以在循环中增加数组索引?,java,arrays,Java,Arrays,所以我的问题是,如果你想得到一个新的计算,我数组中的操作符应该改变。我该怎么做?我现在已经试了好几个小时了:/这就是我所走的路。 因此,首先它以+开始,然后你选择什么来得到一个新的计算,下一个应该是-等等。。。。 所以我需要切换操作符[0] private static void mathGame() { char[] operator = {'+', '-', '*', '/'}; int x; int y = 0; Scanne

所以我的问题是,如果你想得到一个新的计算,我数组中的操作符应该改变。我该怎么做?我现在已经试了好几个小时了:/这就是我所走的路。 因此,首先它以+开始,然后你选择什么来得到一个新的计算,下一个应该是-等等。。。。 所以我需要切换操作符[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之外定义一个变量;然后在}之前将此指令放入循环中,而y==1;:

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


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

如果它回答了你的问题,请选择正确的答案。我总是选择它,但你有一个5分钟的冷却时间:/