text 练习数组Java

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了text 练习数组Java相关的知识,希望对你有一定的参考价值。

package controller;

import java.io.IOException;

import helper.Reader;

public class ArrayExercise2 {

	public static void main(String[] args) throws IOException {
		int input[] = new int[5];
		double summed = 0, average = 0;
		int i=0, odd = 0, even = 0;
		for(i = 0;i<5;i++)
		{
			input[i]=Reader.readInt("\nPlease enter a number ");
		}

		summed = summation(input);
		average = getAverage(input);
		odd = getOdd(input);
		even = getEven(input);
		System.out.println("\nSummation = "+ summed + 
				"\nAverage = " + average + 
				"\nEven = " + even +
				"\nOdd = " + odd);
	}
	
	public static double summation(int input[])
	{
		double sum = 0;
		
		for (int i = 0; i < 5; ++i) {
			sum = sum + input[i];
		}
		return sum;
	}
	
	public static double getAverage(int input[])
	{
		double average = 0;
		
		for (int i = 0; i < 5; ++i) {
			average = average + input[i];
		}
		return average/5;
	}
	
	public static int getOdd(int input[])
	{
		int odd = 0;
		for (int i = 0; i < 5; ++i)
		{
			if(input[i]%2 == 1)
				++odd;
		}
		return odd;
	}
	
	public static int getEven(int input[])
	{
		int even = 0;
		for (int i = 0; i < 5; ++i) 
		{
			if(input[i]%2 == 0)
				++even;	
		}
		return even;
	}
}

以上是关于text 练习数组Java的主要内容,如果未能解决你的问题,请参考以下文章

5.23算法练习最长重复子数组#动态规划

Java 基础语法深度剖析 Java 中的数组含数组练习

Java-数组练习4

Java基础入门-数组练习

图文+视频新手也友好Java一维数组详细讲解(内含练习题答案+详解彩蛋喔~)

Java-数组练习1