I'm an absolute beginner when it comes to C# and tried to do a challenge where I make a system that calculates a cars speed in mph. My code works fine until the last part, where I take the time in seconds, divide by 3600 to get it into hours then do the distance of 1 mile divided by this time for mph. But some of the time, I get infinity, others I just get the completely wrong answer. I've tested small numbers like 2, 3, 4 which give infinity and larger ones like 1000000 that give incorrect answers. Other similar questions had suggestions like changing == in if statements to .Equals(), that doesn't help either.
Here's my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Carspeed
{
class Program
{
static void Main(string[] args)
{
Checkspeed();
}
static void Checkspeed()
{
Console.WriteLine("Has the car passed the first camera? Y/N?");
string check1 = Console.ReadLine();
check1 = check1.ToLower();
if (check1 == "y")
{
Checkspeed2();
}
else
{
Checkspeed();
}
}
static void Checkspeed2()
{
Console.WriteLine("Has the car passed the second camera? Y/N?");
string check2 = Console.ReadLine();
check2 = check2.ToLower();
if (check2 == "y")
{
Speedconv();
}
else
{
Checkspeed2();
}
}
static void Speedconv()
{
Console.WriteLine("How long did it take to get there in seconds?");
int time = Convert.ToInt32(Console.ReadLine());
if (time <= 0)
{
Speedconv();
}
else
{
double timeadj = time / 3600;
Console.WriteLine(1 / timeadj);
Console.ReadLine();
}
}
}
}