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 trialBenson Gathee
Courses Plus Student 1,280 PointsWhat might be wrong with my code? Why doesn't Math.Sqrt work?
Add a public static method named CalculateHypotenuse to the RightTriangle class that returns the length of long side of a right triangle (the hypotenuse), given the length of each of the the two shorter sides (the legs). The Pythagorean theorem will come in handy. The method should take two parameters of type double and return a double.
namespace Treehouse.CodeChallenges
{
class RightTriangle
{
public static double CalculateHypotenuse(double leg1, double leg2)
{
return Math.Sqrt((leg1*leg1)+(leg2*leg2));
}
}
}
2 Answers
andren
28,558 PointsThe Math
class is a part of the System
namespace, to use it you either have to declare that you are using it by including a using
line like this at the top of your code:
using System;
Or just specify System
when accessing the Math
class. In other words call System.Math.Sqrt
instead of Math.Sqrt
.
Benson Gathee
Courses Plus Student 1,280 PointsThanks guys. It works. Apparently, I put the (using System ;) below the namespace instead of above. Really appreciate the help and concern shown
Benson Gathee
Courses Plus Student 1,280 PointsBenson Gathee
Courses Plus Student 1,280 Pointsactually using Systems doesn't work for this case, I tried it! It only works when you put it in-front of the method ie(System.Math.Sqrt(...))
Brendan Whiting
Front End Web Development Techdegree Graduate 84,738 PointsBrendan Whiting
Front End Web Development Techdegree Graduate 84,738 Pointsdid you try
using System;
orusing Systems
?andren
28,558 Pointsandren
28,558 PointsI always test my answers before I post them, I verified that both of the proposed solutions worked in this challenge. As Brendan mentions you have to be sure you type
System
notSystems
and also remember to include the semicolon at the end. It also has to be placed above the class declaration, ideally at the very top of the solution. It won't work if you place it within the class or method.