不能向List中添加多个对象,只添加一个对象
本文关键字:添加 对象 一个对象 List 不能 | 更新日期: 2023-09-27 18:03:53
我试图将汽车添加到汽车列表中,但似乎只添加了一辆汽车。当执行InventoryCars()方法时,它只显示一个对象。
我做错了什么?
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();
}
}
}
}
问题是你一直这样做
Program p = new Program();
你在Menu()
方法中做这个。在Menu()
方法中,您已经处于Program
的作用域中,因此您只需要调用该方法。例如:
else if (GlobalVariables.userSelection == "1")
{
CreateNewPinto(false);
}
删除创建新的程序实例,一切将正常。
好运