2014-01-08 33 views
2

所以我是一個初學編程的人,我不知道它是如何工作的。但這可能是最簡單的問題。 所以我想創建一個銷售東西和東西的程序。客戶正在被導向菜單,在該菜單中他/她將選擇要購買的東西。每個產品都有其指定的字母供用戶輸入。所以我想知道的是,如何使用數組將值分配給標識符,並在客戶選擇相應的字母時調用它們?在C中使用數組而不是數據庫#

像例如, [A]的產品1 [B]的產品2

客戶輸入A,從而產物1將與它一起被調用,並且是產品的價格。然後使用'do-while'循環,程序會詢問客戶是否想要再次進行交易,如果客戶選擇YES,程序將重複,客戶選擇,新項目的價格將爲添加到較舊的一個。 Like: 產品1 +產品2 =總價

然後,如果客戶選擇否,它將打印收據與購買的項目,金額和總額。請幫幫我。如果不是陣列,也許我可以做到這一點,但是。 :(

感謝。 順便說一句,如果你想看到我的代碼,在這兒呢。這僅僅是設計雖然只是給你一個想法。再次感謝!我希望你能幫助我,請。

string ans; 

do 
{ 
    switch(group) 
    { 
    case "S": 
    case "s": 
     Console.WriteLine("***************************************************************"); 
     Console.WriteLine("*       SHINee        *"); 
     Console.WriteLine("***************************************************************"); 
     Console.WriteLine("* Items:       * Bundles for Concert  *"); 
     Console.WriteLine("* | Lanyards/Lace  P200  * [O]BUNDLE 1 P450  *"); 
     Console.WriteLine("*  [A]Onew      *  T-SHIRT(Free Size) *"); 
     Console.WriteLine("*  [B]Jonghyun     *  Lightstick   *"); 
     Console.WriteLine("*  [C]Minho     *  Banner    *"); 
     Console.WriteLine("*  [D]Key      *       *"); 
     Console.WriteLine("*  [E]Taemin     * [P]BUNDLE 2 P590  *"); 
     Console.WriteLine("*         *  T-SHIRT(Free Size) *"); 
     Console.WriteLine("* | Bag Tag   P60  *  2 Lighsticks   *"); 
     Console.WriteLine("*  [G]Onew      *  Banner    *"); 
     Console.WriteLine("*  [H]Jonghyun     *       *"); 
     Console.WriteLine("*  [I]Minho     * [Q]BUNDLE 3 P720  *"); 
     Console.WriteLine("*  [J]Key      *  T-SHIRT(Free Size) *"); 
     Console.WriteLine("*  [K]Taemin     *  2 Lighsticks   *"); 
     Console.WriteLine("*         *  Banner    *"); 
     Console.WriteLine("* | Couple Keychain P90  *  Balloon    *"); 
     Console.WriteLine("*  [M]2Min      *       *"); 
     Console.WriteLine("*  [N]JongKey     *       *"); 
     Console.WriteLine("*         *       *"); 
     Console.WriteLine("*         *       *"); 
     Console.WriteLine("***************************************************************"); 
     Console.WriteLine(""); 
     Console.WriteLine("=========================="); 
     Console.Write("Input letter of choice: "); 

     string merchshin = Console.ReadLine(); 
     break; 
    } 

    Console.Write("Do you want to do another transaction?"); 
    ans = Console.ReadLine(); 
    Console.WriteLine("=========================="); 
    Console.Clear(); 
} 
while (ans == "y" || ans == "Y"); 
Console.ReadLine(); 

回答

0

你所描述什麼是「鍵值對」,對於這個你通常使用Dictionary

例如:

// Create a dictionary of pairs of 'strings' (the item) and 'ints' (the prices) 
Dictionary<string, int> pricesByItem = new Dictionary<string, int>(); 

// Add items and their prices. 
pricesByItem.add("Sandwhich", 3); 
pricesByItem.add("Hamburger", 4); 
pricesByItem.add("Cheese", 5); 

// Get the price of an item 
int priceOfASandwhich = pricesByItem["Sandwhich"]; 
Console.WriteLine(priceOfASandwhich); 
+0

的感謝!我會試試看! :DD – kichen

2

您正在尋找一個鍵值對實現,如dictionary

如果這樣做並不能解決問題,你可以看看像Redis這樣的工具(http://redis.io),它基本上是一個高級的鍵值存儲。

+0

「如果你想排序一個數組,你可以使用'Array.Sort' ...如果這樣不行,試試Google MapReduce!」 :p – sircodesalot

1

我會說使用包含您的產品信息的類的字典。

// Create the dictionary. 
Dictionary<string, Product> productInfo = new Dictionary<string, Product>(); 

//Create products and there information. 
productInfo.Add("ProductOne", new Product { Name = "name", Price=1.99, Description="description" }; 
productInfo.Add("ProductTwo", new Product { Name = "name", Price=1.99, Description="description" }; 
productInfo.Add("ProductThree", new Product { Name = "name", Price=1.99, Description="description" }; 

要創建類,你會只需要

public class Product 
{ 
    //product properties. 
    public string Name { get; set; } 
    public double Price { get; set; } 
    public string Description { get; set; } 

    public Product() 
    { } 
} 

要訪問該字典只使用類似...

Console.WriteLine(string.format("Product Name: {0} Price:{1}", productInfo["ProductOne"].Name, productInfo["ProductOne"].Price)); 
相關問題