Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialSean Flanagan
33,235 PointsAverage
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
231,269 PointsBased 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.