我正在尝试将汽车添加到汽车列表中,但似乎只添加了一辆汽车。 当我执行Inventory Cars()方法时,它只显示一个对象。
我做错了什么?
namespace CarLot
{
class Program
{
List<Car> CarList = new List<Car>();
public static class GlobalVariables
{
public static int numberOfCars;
public static int lotMax = 25;
public static string userSelection;
}
static void Main(string[] args)
{
Program p = new Program();
p.Menu();
}
void InventoryCars()
{
try
{
foreach (var Car in CarList)
{
Console.WriteLine(Car);
}
}
catch
{
Console.WriteLine("Something went Wrong!");
}
Console.ReadLine();
Menu();
}
void CreateNewPinto(bool isHybrid)
{
Pinto myPinto = new Pinto();
if (isHybrid == true)
{
myPinto.hybrid = true;
}
else
{
myPinto.hybrid = false;
}
if (GlobalVariables.numberOfCars < GlobalVariables.lotMax)
{
GlobalVariables.numberOfCars++;
CarList.Add(myPinto);
Console.WriteLine("Pinto Created! Press any key to continue...");
Console.ReadLine();
Menu();
}
else
{
Console.WriteLine("The lot is full! Press any key to continue...");
Console.ReadLine();
Menu();
}
}
static void CreateNewSemiTruck(bool isHybrid)
{
if (GlobalVariables.numberOfCars < GlobalVariables.lotMax)
{
GlobalVariables.numberOfCars++;
}
else
{
Console.WriteLine("The lot is full!");
}
}
static void CreateNewFunnyCar(bool isHybrid)
{
if (GlobalVariables.numberOfCars < GlobalVariables.lotMax)
{
GlobalVariables.numberOfCars++;
}
else
{
Console.WriteLine("The lot is full!");
}
}
static void DriveACar(int carNumber)
{
}
static void HonkACar(int carNumber)
{
}
static void FuelUpACar(int carNumber)
{
}
static void DiscountACar(int carNumber)
{
}
void Menu()
{
Console.WriteLine("-------------------------------------------------------------------------------");
Console.WriteLine("| |");
Console.WriteLine("| Lot Master 3000 |");
Console.WriteLine("| --------------- |");
Console.WriteLine("| |");
Console.WriteLine("| 1) Add Pinto |");
Console.WriteLine("| 2) Add SemiTruck |");
Console.WriteLine("| 3) Add FunnyCar |");
Console.WriteLine("| 4) Add Pinto (Hybrid) |");
Console.WriteLine("| 5) Add SemiTruck (Hybrid) |");
Console.WriteLine("| 6) Add FunnyCar (Hybrid) |");
Console.WriteLine("| |");
Console.WriteLine("| D) Drive a Car |");
Console.WriteLine("| F) Fuel Up a Car |");
Console.WriteLine("| H) Honk a Car |");
Console.WriteLine("| S) Mark car 'On Sale' |");
Console.WriteLine("| |");
Console.WriteLine("| |");
Console.WriteLine("| |");
Console.WriteLine("| L) List all Cars on Lot |");
Console.WriteLine("| |");
Console.WriteLine("| 0) Exit Program |");
Console.WriteLine("| |");
Console.WriteLine("-------------------------------------------------------------------------------");
Console.Write("Enter Selection: ");
GlobalVariables.userSelection = Console.ReadLine().ToUpper();
if (GlobalVariables.userSelection == "0")
{
System.Environment.Exit(0);
}
else if (GlobalVariables.userSelection == "1")
{
Program p = new Program();
p.CreateNewPinto(false);
}
else if (GlobalVariables.userSelection == "2")
{
CreateNewSemiTruck(false);
}
else if (GlobalVariables.userSelection == "3")
{
CreateNewFunnyCar(false);
}
else if (GlobalVariables.userSelection == "4")
{
Program p = new Program();
p.CreateNewPinto(true);
}
else if (GlobalVariables.userSelection == "5")
{
CreateNewSemiTruck(true);
}
else if (GlobalVariables.userSelection == "6")
{
CreateNewFunnyCar(true);
}
else if (GlobalVariables.userSelection == "D")
{
}
else if (GlobalVariables.userSelection == "F")
{
}
else if (GlobalVariables.userSelection == "H")
{
}
else if (GlobalVariables.userSelection == "S")
{
}
else if (GlobalVariables.userSelection == "L")
{
InventoryCars();
}
else
{
Console.WriteLine("Invalid Selection! Enter any key to continue....");
Console.ReadLine();
Menu();
}
}
}
}
答案 0 :(得分:1)
问题在于你一直在做什么
Program p = new Program();
并且您在Menu()
方法中执行此操作。在Menu()
方法中,您已经在Program
的范围内,因此您只需要调用该方法。 e.g:
else if (GlobalVariables.userSelection == "1")
{
CreateNewPinto(false);
}
删除创建新的程序实例,一切都会好的。
祝你好运