Back to Challenge Set 5

Code Challenge: Traffic Light

Difficulty  

divider

The Brief

What they asked for
I'm building a beginner robotics kit and I need the control
logic worked out before I wire anything up. Type in the
color the light is showing, and print what the driver
should do.

Write your requirements list before you write any code. Input, calculate, output, and anything you had to decide for yourself.


Notes

  • Three real colors, so this is a chain: if, else if, else if.
  • You are comparing text, not numbers. Console.ReadLine() already gives you a string — there is nothing to parse here.
  • Compare strings with ==, the same way you compared numbers in Activity 1.13.
  • Something typed in that is not one of the three real colors still needs an answer. That is what the final else is for — decide what it says and write it down in your requirements list.

Where to Start

Only one-star challenges have this section. If you already know how to begin, skip it — and when you move up to two stars, working out the order yourself is part of the job.

  1. Read the color with Console.ReadLine(). No parse needed — it is already text.
  2. Write if (color == "red") first, then chain the other two with else if.
  3. Add a final else for anything that is not red, yellow, or green.
  4. Test all four cases — red, yellow, green, and something else — before you submit.

Sample Output

Sample Output
What color is the light? yellow
Slow down.

Verified in a real run against all four cases — red prints “Stop.”, green prints “Go.”, and anything else falls into your final else.


Submit

Your requirements list and your program file, named cc-traffic-light.

Commence Challenge