本题定义了一个长度为n的boolean型数组b,并给数组元素赋值,要求如果数组元素下标为奇数的话,数组元素值为false,否则为true

本题定义了一个长度为n的boolean型数组b,并给数组元素赋值,要求如果数组元素下标为奇数的话,数组元素值为false,否则为true

int n = 10;
Boolean[] boo = new Boolean[n];
for (int i = 0; i < boo.length; i++) {
if (i % 2 == 0)
boo[i] = true;
else
boo[i] =false;
}

代码如下,有用请采纳


    public static void main(String[] args) {
        //定义了一个长度为10的boolean型数组
        boolean b[] = new boolean[10];
        for (int i = 0; i < 10; i++) {
            if (i%2 != 0){
                //奇数
                b[i] = false;
            }else {
                b[i] = true;
            }
        }
    }

img


        int n = 20;
        boolean[] arr = new boolean[n];
        for(int i=0;i<arr.length;i+=2){
            arr[i] = true;
        }
        System.out.println(Arrays.toString(arr));