f467060571 于 2014.09.28 11:26 提问

using System;
using System.Windows.Forms;
public class MonthActualApp{
double[] rainfall;
double[] difference = new double[12];
public MonthActualApp() {
rainfall = new double[12];
for(int i=0;i<12;i++) {
System.Console.WriteLine("请输入" + (i+1) + "月的降雨量值:");
}
}
public double MonthlyAverageRainfall() {
double sum = 0;
for(int i=0; i<12;i++) {
sum +=rainfall[i];
}
return sum/12;
}
public void computeDifference()
{
for (int i = 0; i < 12; i++)
{
difference[i] = rainfall[i] - MonthlyAverageRainfall();
}
}
public void printArray(double[] aArray) {
string output = "数组元素值分别为:\n";
for(int i=0; i<aArray.Length; i++){
output += aArray[i] + "\n";
}
Console.WriteLine(output);
}
public static void Main(string[] args)
{
MonthActualApp app = new MonthActualApp();
app.printArray(app.rainfall);
app.computeDifference();
app.printArray(app.difference);
string output = string.Format("{0}{1}", "月平均降雨量：",app.MonthlyAverageRainfall() + "mm") + "\n\n";
output += string.Format("{0,-5}{1,-18}{2,-18}","月", "月降雨量", "与月平均降雨量的偏差") +"\n";
for(int i=0; i<12;i++)
{
output += string.Format("{0,-8}{1,-25:n2}{2,-25:n2}",i+1,app.rainfall[i],app.difference[i]) + "\n";
}

``````    Console.WriteLine(output);