2014-01-08 31 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)); 
相关问题