C# C# Basics (Retired) Perfect Wrap Up

Sean Flanagan
Sean Flanagan
33,220 Points

Average

Hi.

I've given the average program a shot. I'm not sure what to put in the try block.

using System;

namespace Treehouse.Average 
{
  class Program 
  {
    static void Main() 
    {
      var runningTotal = 0.0;
      var average = 0.0;

      while (true)
      {
        // Ask user to enter a number or type "done" to see the average
        Console.Write("Please enter a number or type \"done\" to see the average: ");
        var entry = Console.ReadLine();

        if (entry.toLower() == "done")
        {
          break;
          average = runningTotal / average;
        }

        try
        {

        }
        catch
        {
          Console.WriteLine("That is not valid input.");
          continue;
        }
      }
    }
  }
}

1 Answer

Steven Parker
Steven Parker
177,955 Points

Based on the message in the "catch" block, it looks like you want to cover the conversion of the input into a number before performing calculations on it.

So at a minimum, the "try" block should contain the conversion of the input, perhaps something like this (assuming "numericValue" had been previously declared):

        try
        {
            numericValue = Double.Parse(entry);
        }

The "try" block can optionally also contain any other code that should be performed only if the conversion succeeds.