Hi Guys,
iI am new to programming and started aabout 2 months ago. I am doing some exercises to help me get my head around the programming language. Here is what i am stuck on;
Write a program that uses nested loops to collect data and calculate the average rainfall over a period of years. First the program should ask for the number of years. The outer loop will iterate once for each year. The inner loop will iterate 12 times, once for each month. Each iteration of the inner loop will ask the user for the inches of rainfall for that month. After all iterations, the program should display the number of months, the total inches of rainfall, and the average rainfall per month for the entire period. Use Methods to solve the problem.
Here is the code i have wrote out so far. The bit i am stuck on is how do i get the outter loop to work for the number of years the user has entered.My coding is posted below
static void Main(string[] args)
` {`
int Numyears = 0;
double Rainfall = 0;
double avrRainfall = 0.0;
string[] Month = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
Console.WriteLine("please enter number of years");
Numyears = int.Parse(Console.ReadLine());
while(Numyears < 1)
{
Console.WriteLine("Minimum number of years is 1 Please re-enter");
Numyears = int.Parse(Console.ReadLine());
}
for (int i = 0; i <= Numyears; Numyears++)
{
for (int j = 0; j < Month.Length; j++)
{
Console.WriteLine(" Enter inches of rainfall for {0}", Month [j]);
Console.ReadLine();
}
}