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);
	}
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章