I wrote the following code which prompts the user to enter a (road) speed limit and speed of car. The program works except for when the speed of car is less than the speed limit. I highlighted the else if block that should catch this. Can't figure out why. Any ideas?
C#:
using System;
namespace SpeedingCamera
{
class Program
{
/*3- 4- Your job is to write a program for a speed camera. For simplicity, ignore the details such as camera, sensors,
etc and focus purely on the logic. Write a program that asks the user to enter the speed limit. Once set, the program
asks for the speed of a car. If the user enters a value less than the speed limit, program should display Ok on the
console. If the value is above the speed limit, the program should calculate the number of demerit points. For every
5km/hr above the speed limit, 1 demerit points should be incurred and displayed on the console. If the number of
demerit points is above 12, the program should display License Suspended.
*/
static void Main(string[] args)
{
var demeritPoints = 0;
Console.WriteLine("Please enter the speed limit");
string num1 = Console.ReadLine();
//Convert string to integer
int speedLimit = Convert.ToInt32(num1);
Console.WriteLine("Please enter the speed of the car");
string num2 = Console.ReadLine();
//Convert string to integer
int speedOfCar = Convert.ToInt32(num2);
Console.WriteLine("speedOfCar: " + speedOfCar);
Console.WriteLine("speedLimit: " + speedLimit);
if ( speedOfCar < speedLimit)
{
Console.WriteLine("Ok");
}
else if ( speedOfCar > speedLimit)
{
demeritPoints = speedOfCar - speedLimit;
Console.WriteLine("You were going " + demeritPoints + " over the speed limit");
}
[B]else if (speedOfCar < speedLimit)
{
demeritPoints = speedLimit - speedOfCar;
Console.WriteLine("You were going " + demeritPoints + " under the speed limit");
}[/B]
else
{
Console.WriteLine("You were going the speed limit");
}
// Added these 2 lines to keep cmd window from closing before I can see response.
Console.WriteLine("Press enter to close...");
Console.ReadLine();
}
}
}