【发布时间】: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