zoukankan      html  css  js  c++  java
  • Java之奇偶组合

     写一个函数,将已知数组的奇数项组合成一个新的数组,在函数中调用该数组,并且输出新数组的内容。
     定义一个数组,该数组为{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18}
     写一个函数,函数名为oddArray需要一个参数,该函数的功能是将数组中的奇数项存入另外一个数组,并且返回该数组到主函数中。
     在主函数中定义一个新的数组,用于获取oddArray函数的返回值,然后循环显示返回的值,显示为1 3 5 7 9 11 13 15 17

    package com.cdp.shuzu;
    
    public class shuzu {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		System.out.println("奇数偶数组合:");
    		// 数组
    		int arry1[] = { 1, 2, 3, 4, 5,
    				6, 7, 8, 9, 10, 11, 12,
    				13, 14, 15, 16,17, 18 };
    		// 定义一个oddArray函数的数组
    		int arry2[] = oddArray(arry1);
    		//
    		for (int i = 0; i < arry2.length; i++) {
    			System.out.print(arry2[i] + " ");
    		}
    
    	}
    
    	private static int[] oddArray(int[] arry1) {
    		int len = 0;
    		if (arry1.length % 2 == 0) {
    			len = arry1.length / 2;
    		} else {
    			len = arry1.length / 2 + 1;
    		}
    		int[] arry2 = new int[len];
    		for (int i = 0, j = 0; j < len; i = i + 2, j++) {
    			arry2[j] = arry1[i];
    		}
    		return arry2;
    
    	}
    
    }
    

    run:

    奇数偶数组合:
    1 3 5 7 9 11 13 15 17 
    

      

    不努力,还要青春干什么?
  • 相关阅读:
    The password has to have a minimum of 6 characters, including at least 1 small letter, 1 uppercase letter and 1 number
    Angular i18n的技术分享、踩过的坑
    转: .Net 4.0 ExpandoObject 使用
    min_square
    KALMAN PYTHON
    双系统安装 win + ubuntu
    docker
    drl
    shell
    导航定位方案
  • 原文地址:https://www.cnblogs.com/caidupingblogs/p/5855566.html
Copyright © 2011-2022 走看看