我的程序有问题或 netbeans 错误

Problems with my program or netbeans error

我正在做一个练习,其中你问我们以下问题:练习 06:读取对应于两个表的 12 个数值元素的数据,并将它们混合成三分之一的形式:3 来自表 A,3 来自B,A 中的其他 3 个,B 中的另外 3 个,等等。在编写代码时(根据我的说法,这很好)我在 netbeans 中遇到错误(附上照片)你能告诉我错误的原因是什么?我还是个学生。非常感谢您的阅读(附上我的代码和错误图片)。

package ejercicioarreglos_06;


import java.util.Scanner;

public class EjercicioArreglos_06 {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
     
        int tablaA[] = new int[12];
        int tablaB[] = new int[12];
        int contador = 0;
        boolean eleccion = true;
        int contA = 0, contB = 0;

        for (int i = 0; i < tablaA.length; i++) {
            System.out.print("Ingresa el valor " + (i + 1) + " de la tabla A: ");
            tablaA[i] = in.nextInt();

        }
        for (int j = 0; j < tablaB.length; j++) {
            System.out.print("Ingrese el valor " + (j + 1) + " de la tabla B: ");
            tablaB[j] = in.nextInt();

        }

        for (int k = 0; k < tablaB.length + tablaB.length; k++) {
            if (eleccion = true) {

                System.out.println(tablaA[contA]);
                contador++;
                contA++;
                if (contador > 2) {
                    eleccion = false;
                }
            } else {
                System.out.print(tablaB[contB]);
                contador--;
                contB++;
                if (contador < 0) {
                    eleccion = true;
                }
            }

        }

    }

}

根据Javadoc

ArrayIndexOutOfBoundsException

抛出表示已使用非法索引访问数组。索引为负数或大于或等于数组的大小。

ArrayIndexOutOfBoundsException

这是一个异常(错误),当我们提供的索引超出允许访问数组中元素的限制时就会发生。请记住,Java 索引从 0 开始,一直到元素数 -1。

注意 public class ArrayIndexOutOfBoundsException 在 Java 平台的 class 层次结构中的位置:

-> java.lang.Object

--> java.lang.Throwable

---> java.lang.Exception

----> java.lang.RuntimeException

-----> java.lang.IndexOutOfBoundsException

------> java.lang.ArrayIndexOutOfBoundsException

这是一个我们尝试使用无效索引访问数组元素的示例:

public class Test{
  public static void main(String args[]){
    // an array of five elements
    int[] values = {8, 98, 100, 3, 14};     

    // we will provide an invalid index
    System.out.println(values[5]); 

    System.exit(0);
  }
}

这段代码编译正常。但是,当我们尝试 运行 它时,我们会收到以下错误消息:

Exception in thread "main" 
java.lang.ArrayIndexOutOfBoundsException: 4
  at Study.main(Test.java:7)

更正此错误的最合适方法是提供真正在允许范围内的索引值。

If condition available 似乎总是正确的,我更正了它以使代码工作,即来自 if (eleccion = true)if (eleccion)

package ejercicioarreglos_06;


import java.util.Scanner;

public class EjercicioArreglos_06 {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
     
        int tablaA[] = new int[12];
        int tablaB[] = new int[12];
        int contador = 0;
        boolean eleccion = true;
        int contA = 0, contB = 0;

        for (int i = 0; i < tablaA.length; i++) {
            System.out.print("Ingresa el valor " + (i + 1) + " de la tabla A: ");
            tablaA[i] = in.nextInt();

        }
        for (int j = 0; j < tablaB.length; j++) {
            System.out.print("Ingrese el valor " + (j + 1) + " de la tabla B: ");
            tablaB[j] = in.nextInt();

        }

        for (int k = 0; k < tablaB.length + tablaB.length; k++) {
            if (eleccion) {

                System.out.println(tablaA[contA]);
                contador++;
                contA++;
                if (contador > 2) {
                    eleccion = false;
                }
            } else {
                System.out.print(tablaB[contB]);
                contador--;
                contB++;
                if (contador < 0) {
                    eleccion = true;
                }
            }

        }

    }

}