我想寫一個模擬汽車競賽比賽的程序,用戶在比賽中插入汽車的數量和每輛車的時間。該計劃將以最快的時間打印車輛,並以最快的時間打印車輛。我的編譯器總是說我沒有分配我的變量
所以我寫了這個代碼:
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();
}
}
}
我得到這個錯誤:
Use of unassigned local variable 'second'
我不明白爲什麼我得到這個錯誤。
請顯示完整的堆棧跟蹤 –
它有助於顯示哪行代碼導致相對於帖子的錯誤。 – ChiefTwoPencils
C#編譯器不允許您使用未分配的本地變量。你的「for」從來沒有用過?編譯器無法知道這一點。這並不聰明。 –