2014-05-17 35 views
-4

我想將汽車添加到汽車列表,但只有一輛汽車有史以來似乎被添加。 當我執行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(); 
      } 
     } 
    } 
} 
+0

使用[開關](http://msdn.microsoft.com/en-us/library/06tc147t.aspx)的情況下爲用戶選擇字符串。至於你的問題,給我5分鐘。 –

+0

開關盒是否更有效? – CryptoJones

+0

放置一個斷點並逐步完成代碼。您正在遍歷'InventoryCars'中的'CarList',所以如果只顯示一行,那麼您只能在列表中有一輛車。 –

回答

1

的問題是,你繼續做

Program p = new Program(); 

和你正在做這裏面Menu()方法。在Menu()方法中,您已經在Program的範圍內,因此您只需調用該方法即可。例如:

else if (GlobalVariables.userSelection == "1") 
{ 
    CreateNewPinto(false); 
} 

刪除程序的新實例的創建,並且全部都會正常。

好運

+0

當我這樣做時,它抱怨「對象引用是非靜態字段,方法或屬性所必需的。」 – CryptoJones

+0

CryptoJones - 你複製粘貼上面給出的代碼嗎?如果你這樣做了,那麼你所要做的就是刪除「Program p = new Program();」和「p」。在「1」和「4」的if語句中。再試一次,告訴我它是怎麼回事。並請刪除您給我的「-1」 –

+1

抱歉。我誤解了。我刪除了'Program p = new Program();'來自主函數,而不是CreatePinto();按照您的建議操作。 當我做你建議我的列表現在包含所有的項目! 謝謝Shahar Eldad! – CryptoJones

相關問題