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 trialElfar Oliver
3,924 PointsI've done the ifs I don't know what to return Cannot implicitly convert type 'double' to 'string'
The error Cannot implicitly convert type 'double' to 'string' always pops up and I don't know what to convert
using System;
class Program
{
static string CheckSpeed(double speed)
{
if (speed > 65)
{
Console.WriteLine("too fast");
}
else if (speed < 45)
{
Console.WriteLine("too slow");
}
else
{
Console.WriteLine("speed OK");
}
return speed;
}
static void Main(string[] args)
{
// This should print "too slow".
Console.WriteLine(CheckSpeed(44));
// This should print "too fast".
Console.WriteLine(CheckSpeed(88));
// This should print "speed OK".
Console.WriteLine(CheckSpeed(55));
}
}
1 Answer
Steven Parker
231,269 PointsThe CheckSpeed method is defined as returning a string, but it tries to return speed, which is a double. That's what the compiler is complaining about.
Re-write it to make it return the strings instead of writing them to the console (that part is done in Main
).
Elfar Oliver
3,924 PointsElfar Oliver
3,924 PointsJust to be sure, I keep the ifs out of the main?
Steven Parker
231,269 PointsSteven Parker
231,269 PointsYou won't modify
Main
for this challenge. All the work should be done in the body ofCheckSpeed
.Elfar Oliver
3,924 PointsElfar Oliver
3,924 PointsIn the body of CheckSpeed or should I alter the static string CheckSpeed?
Elfar Oliver
3,924 PointsElfar Oliver
3,924 PointsI could really use a hint right about now. I am no where nearer than before
Steven Parker
231,269 PointsSteven Parker
231,269 PointsYour basic structure was pretty good, but you need to return the strings instead of writing them to the console. For example, instead of this:
... you might write this:
Elfar Oliver
3,924 PointsElfar Oliver
3,924 PointsThank you very much. As always I was just one step away(from throwing my laptop out the window)
So your "Re-write it to make it return the strings instead of writing them to the console" was actually the answer and not some huge hint