題目:創建一個String變量數組並用1月-12月的月份名稱初始化該數組,再創建一個包含12個0.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);
}
}