カーレース競技をシミュレートするプログラムを作成しようとしています。ユーザーは、競技中の車の数と各車の時間を挿入します。プログラムは、最速タイムの車と 2 番目に速いタイムの車を出力します。
だから私はこのコードを書いた:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int numc, first, second, time, i, temp;
Console.WriteLine("Please insert the number of cars in the competition");
numc = int.Parse(Console.ReadLine());
Console.WriteLine("Please insert the time it took the car to finish the race");
time = int.Parse(Console.ReadLine());
first = time;
for (i = 0; i < numc; i++)
{
Console.WriteLine("Please insert the time it took the car to finish the race");
time = int.Parse(Console.ReadLine());
if(time<first)
{
temp=first;
first = time;
second = temp;
}
}
Console.WriteLine("The time of the car who got first place is:" +first);
Console.WriteLine("The time of the car who got second place is:" +second);
Console.ReadLine();
}
}
}
次のエラーが表示されます。
割り当てられていないローカル変数 'second' の使用
なぜこのエラーが発生するのかわかりません。