定义一个函数,函数有两个整型参数,函数的功能是计算并输出两个参数之间的奇数个数 和 偶数和。

package com.cool;

public class Test3 {

	/**
	 * 定义一个函数,函数有两个整型参数,函数的功能是计算并输出两个参数之间的奇数个数 和 偶数和。
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		A(1, 10);

	}

	static void A(int a, int b) {
		int count = 0;
		int sum = 0;
		for (int i = Math.min(a, b); i <= Math.max(a, b); i++) {
			if (i % 2 != 0) {
				count++;
			} else {
				sum += i;
			}
		}
		System.out.println("奇数的个数:" + count);
		System.out.println("偶数和为:" + sum);
	}

}

你可能感兴趣的:(Java)