`

JAVA每日一题02

阅读更多

题目:创建一个String变量数组并用1-12月的月份名称初始化该数组,再创建一个包含120.0-100.00之间的随机十进制的数组,然后将每个月份的名字连同相应的十进制值显示出来,最后计算并显示这个12个十进制值的平均值。

 新的解题技巧还需指点哦

 

package com.tengfei.lesson01;
public class Months {
	public static void main(String args[]) {
		// Initialize the months' array with names of the months of the year:
		String[] monthNames = { "January", "February", "March", "April", "May",
				"June", "July", "August", "September", "October", "November",
				"December" };

		double average = 0.0; // A variable used to find the average.

		// Declare an array which can contain the 12 random numbers:
		double[] numbers = new double[12];

		// Fill in the numbers' array with 12 numbers between 0.0 and 100.0, and
		// print it out,
		// alongside each month. Also keep track of the sum of those numbers
		// elements.

		for (int i = 0; i < numbers.length; i++) {
			numbers[i] = 100.0 * Math.random();
			System.out.println("In Month " + monthNames[i] + " number is "
					+ numbers[i]);
			average += numbers[i];
		}
		average /= numbers.length;

		System.out.println("\nAverage of values in numbers is " + average);
	}
}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics