我必须编写一个程序,询问用户年数,然后询问用户这些年中每个月的降雨量(毫米)。我必须计算月份总数,降雨量的总英寸数,每月的平均降雨量,计算所有月份的最大降雨量,并输出月份名称(将月份编号转换为名称)和降雨量最大的年份。到目前为止,我已经编写了此代码,但是即使我计算出最高的降雨量值,也无法弄清楚如何准确输出降雨量最高的确切月份名称和年份。

const int numMonths = 12;
int numYears, months, largest = 0;
double sum = 0;


cout << "Please enter the number of years: ";
cin >> numYears;
cin.ignore();

for (int years = 1; years <= numYears; years ++)
{
    for (int months = 1; months <= numMonths; months ++)
    {
    double rain;
    cout << "Please enter the rainfall in mm for year " << years << ", month " << months << "\n";
    cin >> rain;
    sum += rain;
    if (rain > largest){

        largest = rain;

    }
    cin.ignore();
    }
}

int totalMonth = numYears*numMonths;
double avgRain = sum / totalMonth;
cout << "Total number of months: " << totalMonth << "\n";
cout << "Total inches of rainfall for the entire period: "<< sum << "\n";
cout << "Average rainfall per month for the entire period: " << avgRain << "\n";
cout << "Highest rainfall was " << largest << ;






cin.get();
return 0;

最佳答案

怎么样:

   if (rain > largest_rain){
        largest_rain = rain;
        largest_month = months;
        largest_year = years;
    }

08-24 18:17