求出数组中索引与索引对应的元素都是奇数的元素

分析:

1、遍历数组

2、判断索引是否是奇数(索引 % 2 != 0)

3、判断索引对应的元素是否是奇数(arr[索引] % 2 != 0)

4、满足条件输出结果

package day04.work;

import java.util.Scanner;

public class Test02 {
    public static void main(String[] args) {
        //求索引与索引对应的元素都是 奇数
        Scanner sc = new Scanner(System.in);
        int arr[] = new int[5];
        for (int i = 0; i < 5; i++) {
            System.out.println("请输入值:");
            arr[i] = sc.nextInt();
        }
        //判断索引与对应值都为奇数
        //定义计数器,防止全不是奇数
        int conit =0;
        for (int j = 0; j < 5; j++) {
            if (j%2!=0&&arr[j]%2==1){
                System.out.println("索引为:"+(j)+"数值为:"+arr[j]);
                conit++;
            }
        }
        if (conit==0){
            System.out.println("此数组无符合条件的数!");
        }
    }
}

猜你喜欢

转载自blog.csdn.net/lzj_love_wx/article/details/114371819