【问题标题】:User input looping and adding all inputs用户输入循环并添加所有输入
【发布时间】:2022-01-05 13:13:03
【问题描述】:

我需要获取所有用户输入产品的总数,但我做不到。我需要让用户选择产品及其数量,然后询问用户是否愿意添加更多产品或付款。用户将再次输入产品及其数量。之后程序会再次询问是否添加或支付。当用户选择支付时,它应该输出产品的总金额。最后程序会询问用户是否想再次使用该程序,如果没有,它将退出。

int choice, quanti, decide, total, price;
string w = "WELCOME ";
Console.SetCursorPosition((Console.WindowWidth - w.Length) / 2, Console.CursorTop);  // for setting string output on center top
Console.WriteLine(w);
Console.WriteLine("");
System.Threading.Thread.Sleep(2000); //time delay

string p = "HERE'S OUR MERCHANDISES! ";
Console.SetCursorPosition((Console.WindowWidth - p.Length) / 2, Console.CursorTop);  // for setting string output on center top
Console.WriteLine(p);
System.Threading.Thread.Sleep(2000);//time delay

string[] products = { "[1]BLACKPINK Lightstick ", "[2]DREAMCATCHER Seasons Greetings", 
                        "[3]RED VELVET Summer Package"};

for (int g = 0; g < products.Length; g++)
{
    Console.WriteLine(products[g]);
}

for (int i = 0; i < products.Length; i++)
{
    Console.Write("Pick your product: ");
    choice = int.Parse(Console.ReadLine());
    switch (choice)
    {
        case 1:
            Console.WriteLine("BLACKPINK Lightstick 1500php");

            break;
        case 2:
            Console.WriteLine("DREAMCATCHER Seasons Greetings 920php");

            break;
        case 3:
            Console.WriteLine("RED VELVET Summer Package 980php");

            break;
    }
    Console.WriteLine("Quantity of product: ");
    quanti = int.Parse(Console.ReadLine());
    System.Threading.Thread.Sleep(2000);//time delay
    Console.WriteLine("[1] Add more products \t [2] Pay: ");
    decide = int.Parse(Console.ReadLine());

    if (decide == 2)
    {
        decide++;
        if (choice == 1)
        {
            price = 1500;
        }
        else if (choice == 2)
        {
            price = 920;
        }

        else if (choice == 3)
        {
            price = 980;
        }
        else
        {
            break;
        }

        total = choice * price * quanti;
        Console.Write(total);
    }
    else
    {
        Console.Write("Pick your product: ");
        choice = int.Parse(Console.ReadLine());
        switch (choice)
        {
            case 1:
                Console.WriteLine("BLACKPINK Lightstick 1500php");
                price = 1500;
                break;
            case 2:
                Console.WriteLine("DREAMCATCHER Seasons Greetings 920php");
                price = 920;
                break;
            case 3:
                Console.WriteLine("RED VELVET Summer Package 980php");
                price = 700;
                break;

        }
        Console.WriteLine("Quantity of product: ");
        quanti = int.Parse(Console.ReadLine());
        System.Threading.Thread.Sleep(2000);//time delay
        Console.WriteLine("[1] Add more products \t [2] Pay: ");
        decide = int.Parse(Console.ReadLine());
        if (decide == 2)
        {
            if (choice == 1)
            {
                price = 1500;
            }
            else if (choice == 2)
            {
                price = 920;
            }
            else if (choice == 3)
            {
                price = 980;
            }
            else
            {
                break;
            }

            total = price * quanti;
            Console.Write(total);
        }
    }
}

【问题讨论】:

  • 你有什么期望,你的程序给你什么?
  • 我重新格式化了您的代码并从末尾删除了三个无关的右大括号。当decide == 2 时,似乎total = choice * price * quanti; 应该是total = price * quanti;,就像decide != 2 时一样,因为choice 是选定的菜单项。您的第二个for 循环可能应该是一个while/do ... while 循环,它会提示您添加产品,直到用户选择付款。

标签: c# arrays .net loops console-application


【解决方案1】:

检查这是否是您所期望的。

int choice, quanti, decide, total, price;
        while (true)
        {
            Console.Clear();
            string w = "WELCOME ";
            Console.SetCursorPosition((Console.WindowWidth - w.Length) / 2, Console.CursorTop);  // for setting string output on center top
            Console.WriteLine(w);
            Console.WriteLine("");
            System.Threading.Thread.Sleep(2000); //time delay

            string p = "HERE'S OUR MERCHANDISES! ";
            Console.SetCursorPosition((Console.WindowWidth - p.Length) / 2, Console.CursorTop);  // for setting string output on center top
            Console.WriteLine(p);
            System.Threading.Thread.Sleep(2000);//time delay

            string[] products = { "[1]BLACKPINK Lightstick ", "[2]DREAMCATCHER Seasons Greetings",
                    "[3]RED VELVET Summer Package"};

            for (int g = 0; g < products.Length; g++)
            {
                Console.WriteLine(products[g]);
            }

            Dictionary<int, int> ProductList = new Dictionary<int, int>();
            while (true)
            {
                {
                    Console.Write("Pick your product: ");
                    choice = int.Parse(Console.ReadLine());
                    switch (choice)
                    {
                        case 1:
                            Console.WriteLine("BLACKPINK Lightstick 1500php");
                            break;
                        case 2:
                            Console.WriteLine("DREAMCATCHER Seasons Greetings 920php");

                            break;
                        case 3:
                            Console.WriteLine("RED VELVET Summer Package 980php");

                            break;
                    }
                    Console.WriteLine("Quantity of product: ");
                    quanti = int.Parse(Console.ReadLine());
                    if (!ProductList.ContainsKey(choice))
                        ProductList.Add(choice, quanti);
                    else
                        ProductList[choice] += quanti;
                    System.Threading.Thread.Sleep(2000);//time delay
                    Console.WriteLine("[1] Add more products \t [2] Pay: ");
                    decide = int.Parse(Console.ReadLine());

                    if (decide == 2)
                        break;
                }
            }
            total = 0;
            foreach (int key in ProductList.Keys)
                switch (key)
                {
                    case 1:
                        total += ProductList[key] * 1500;
                        break;
                    case 2:
                        total += ProductList[key] * 920;
                        break;
                    case 3:
                        total += ProductList[key] * 980;
                        break;
                    default:
                        total += 0;
                        break;
                };
            Console.WriteLine("To Pay: " + total.ToString());

            Console.WriteLine("Another shopping? [1]Yes [2] No");
            choice = int.Parse(Console.ReadLine());
            if (choice == 2)
                break;
        }
    }

【讨论】:

  • 先生,我已经尝试了您的答案,并且有效,我刚刚添加了折扣选项。如果不是太多要求,我需要在最后输出收据之类的东西,就像它会输出用户购买的东西,请问我该怎么做?我尝试输出用于获取用户输入的变量,但它只接收用户购买的第一个产品,它无法获得用户获得的其他产品。
  • 在代码中,收据的产品列表在 ProductList 变量中,总值在 total 变量中。现在是您希望如何计算折扣的问题。如果您想为某些产品添加折扣,您可以在计算总值的循环中执行此操作,如果您想在循环后将折扣添加到您需要执行此操作的总价值中。但是,如果您想要最后一张收据的折扣,您需要声明额外的变量来存储所有客户的收据
  • 那我只需要输出ProductList,这样我就可以输出所有用户购买的产品和总数?
  • 先生,我可以这样输出吗?购买的产品 - 数量 - 总计 - 现金 - 零钱
  • 查看下一个答案
【解决方案2】:
struct product
    {
        public string Name;
        public float Price;
        public float Discount;
    }
    static void Main(string[] args)
    {


        int choice, quanti, decide;
        float total;
        Dictionary<int,product> products=new Dictionary<int, product>();
        products.Add(1,new product() { Name = "BLACKPINK Lightstick", Price = 1500, Discount = 0 });
        products.Add(2, new product() { Name = "DREAMCATCHER Seasons Greetings", Price = 920, Discount = 0 });
        products.Add(3, new product() { Name = "RED VELVET Summer Package", Price = 980, Discount = 0 });
        while (true)
        {
            Console.Clear();
            string w = "WELCOME ";
            Console.SetCursorPosition((Console.WindowWidth - w.Length) / 2, Console.CursorTop);  // for setting string output on center top
            Console.WriteLine(w);
            Console.WriteLine("");
            System.Threading.Thread.Sleep(2000); //time delay
            string p = "HERE'S OUR MERCHANDISES! ";
            Console.SetCursorPosition((Console.WindowWidth - p.Length) / 2, Console.CursorTop);  // for setting string output on center top
            Console.WriteLine(p);
            System.Threading.Thread.Sleep(2000);//time delay
            foreach (int key in products.Keys)
            {
                Console.WriteLine("["+key.ToString()+"]"+products[key].Name);
            }

            Dictionary<int, int> ProductList = new Dictionary<int, int>();
            while (true)
            {
                {
                    Console.Write("Pick your product: ");
                    choice = int.Parse(Console.ReadLine());
                    Console.WriteLine(products[choice].Name+" "+products[choice].Price.ToString()+"php");
                    Console.WriteLine("Quantity of product: ");
                    quanti = int.Parse(Console.ReadLine());
                    if (!ProductList.ContainsKey(choice))
                        ProductList.Add(choice, quanti);
                    else
                        ProductList[choice] += quanti;
                    System.Threading.Thread.Sleep(2000);//time delay
                    Console.WriteLine("[1] Add more products \t [2] Pay: ");
                    decide = int.Parse(Console.ReadLine());

                    if (decide == 2)
                        break;
                }
            }
            total = 0;
            Console.WriteLine("------------------------------------");
            foreach (int key in ProductList.Keys)
            {
                total += products[key].Price*ProductList[key];
                Console.WriteLine(products[key].Name.PadRight(40,' ')+ProductList[key].ToString().PadLeft(3,' ')+ " * "+products[key].Price.ToString().PadLeft(10,' '));
                Console.WriteLine((products[key].Price * ProductList[key]).ToString().PadLeft(56, ' '));
            }
            Console.WriteLine(("To Pay: " + total.ToString()).PadLeft(56,' '));
            Console.WriteLine("Cash: ");
            var cash = float.Parse(Console.ReadLine());
            Console.WriteLine(("Change: " + (cash - total).ToString()).PadLeft(56, ' '));

            Console.WriteLine("Another shopping? [1]Yes [2] No");
            choice = int.Parse(Console.ReadLine());
            if (choice == 2)
                break;
        }
    }

【讨论】:

猜你喜欢
  • 2014-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-30
  • 2013-11-16
  • 2020-02-15
相关资源
最近更新 更多