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 trial

C#

C# I cannot understand why the code does not work

Ok this is the basic course of c# the final task.

We've updated our previous CheckSpeed method to return a string value. Update the method body with this logic:

If the speed parameter is over 65, return the string "too fast". If the speed parameter is under 45, return the string "too slow". Otherwise, return the string "speed OK".

I cannot understand why it wont accept my solution since it works fine on visual basic 2019 My solution is as follows

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 Convert.ToString(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));
}

}

The error I get from that code is Bummer: too fast. We called CheckSpeed(65.5), but we got a return value of: "65.5". We were expecting "too fast".

Jesús Armenta
Jesús Armenta
13,505 Points

Your program works fine but the instruction given was to return just the message not the conversion of the speed parameter in the end, so just return the message instead of Console.WriteLine in the if/else statements.

Ah yes, thank you guys. I just needed to return the strings directly. Much appreciated!!

1 Answer

Steven Parker
Steven Parker
231,271 Points

There seems to be 3 issues:

  • the function should not return the speed itself
  • the function should return the messages instead of writing them our (the main modules does that)
  • the messages should be exactly as given in the instructions, no added punctuation or case changes