在c#控制台应用程序中按下按钮时添加项目
我一直在开发一个小的控制台应用程序,它有一个项目列表,当按下一个数字时,相关的项目应该计算到总数(已经完成) 问题在于如何将这些项添加到列表视图并在控制台应用程序中显示它们。这就是我到目前为止所做的在c#控制台应用程序中按下按钮时添加项目,c#,.net,C#,.net,我一直在开发一个小的控制台应用程序,它有一个项目列表,当按下一个数字时,相关的项目应该计算到总数(已经完成) 问题在于如何将这些项添加到列表视图并在控制台应用程序中显示它们。这就是我到目前为止所做的 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace SuperMarcado { class MainClass { publ
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SuperMarcado
{
class MainClass
{
public static void Main(string[] args)
{
ShoppingCart myCart = new ShoppingCart();
Product[] shopProducts = new Product[]
{
new Product("Cheese", 6, "Milk Cheese", "3/12/2016", 4),
new Product("Bread", 2, "Grain Bread", "27/11/2016", 8),
new Product("Ice Cream", 10, "Ice Cream", "09/11/2001", 1),
new Product("Cookies", 100, " Chocolate Cookies", "00/00/0000", 5),
new Product("Biscuits", 0.25f, "Vanila ", "08/01/2006", 6)
};
Shop shop = new Shop(shopProducts);
shop.DisplayProducts();
Console.WriteLine("------------------------------------------------------------");
myCart.printY = Console.CursorTop;
myCart.Display();
ConsoleKeyInfo input;
while ((input = Console.ReadKey(true)).Key != ConsoleKey.Escape)
{
if (!Char.IsDigit(input.KeyChar))
return;
int index = Convert.ToInt16(input.KeyChar.ToString()) - 1;
if (index < 1 || index > shop.products.Length)
return;
myCart.AddProduct(shop.products[index]);
shop.DecreaseAmount(shop.products[index]);
shop.DisplayProducts();
myCart.Display();
int userInput = 0;
do
{
userInput = ShoppingCart();
} while (userInput != 5);
}
}
static public int ShoppingCart()
{
Console.WriteLine("Your cart");
Console.WriteLine();
var result = Console.ReadLine();
return Convert.ToInt32(result);
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
命名空间超级商场
{
类主类
{
公共静态void Main(字符串[]args)
{
ShoppingCart myCart=新建ShoppingCart();
产品[]shopProducts=新产品[]
{
新产品(“奶酪”,6,“牛奶奶酪”,“3/12/2016”,4),
新产品(“面包”,2,“谷物面包”,“2016年11月27日”,8),
新产品(“冰淇淋”,10,“冰淇淋”,“09/11/2001”,1),
新产品(“饼干”,100,“巧克力饼干”,“00/00/0000”,5),
新产品(“饼干”,0.25f,“瓦尼拉”,“08/01/2006”,6)
};
商店=新商店(商店产品);
shop.DisplayProducts();
Console.WriteLine(“-------------------------------------------------------------------------------------”);
myCart.printY=Console.CursorTop;
myCart.Display();
ConsoleKeyInfo输入;
while((input=Console.ReadKey(true)).Key!=ConsoleKey.Escape)
{
if(!Char.IsDigit(input.KeyChar))
返回;
int index=Convert.ToInt16(input.KeyChar.ToString())-1;
if(索引<1 | |索引>商店.产品.长度)
返回;
myCart.AddProduct(shop.products[index]);
商店减价金额(商店产品[指数]);
shop.DisplayProducts();
myCart.Display();
int userInput=0;
做
{
userInput=ShoppingCart();
}while(userInput!=5);
}
}
静态公共int ShoppingCart()
{
Console.WriteLine(“您的购物车”);
Console.WriteLine();
var result=Console.ReadLine();
返回Convert.ToInt32(结果);
}
}
}
首先,您的问题是第二个循环,请删除它,它应该可以正常工作
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SuperMarcado
{
class MainClass
{
public static void Main(string[] args)
{
ShoppingCart myCart = new ShoppingCart();
Product[] shopProducts = new Product[]
{
new Product("Cheese", 6, "Milk Cheese", "3/12/2016", 4),
new Product("Bread", 2, "Grain Bread", "27/11/2016", 8),
new Product("Ice Cream", 10, "Ice Cream", "09/11/2001", 1),
new Product("Cookies", 100, " Chocolate Cookies", "00/00/0000", 5),
new Product("Biscuits", 0.25f, "Vanila ", "08/01/2006", 6)
};
Shop shop = new Shop(shopProducts);
shop.DisplayProducts();
Console.WriteLine("------------------------------------------------------------");
myCart.printY = Console.CursorTop;
myCart.Display();
ConsoleKeyInfo input;
while ((input = Console.ReadKey(true)).Key != ConsoleKey.Escape)
{
if (!Char.IsDigit(input.KeyChar))
return;
int index = Convert.ToInt16(input.KeyChar.ToString()) - 1;
if (index < 1 || index > shop.products.Length)
return;
myCart.AddProduct(shop.products[index]);
shop.DecreaseAmount(shop.products[index]);
shop.DisplayProducts();
myCart.Display();
//int userInput = 0;
//do
//{
// userInput = ShoppingCart();
//} while (userInput != 5);
}
}
//static public int ShoppingCart()
//{
// Console.WriteLine("Your cart");
// Console.WriteLine();
// var result = Console.ReadLine();
// return Convert.ToInt32(result);
//}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
命名空间超级商场
{
类主类
{
公共静态void Main(字符串[]args)
{
ShoppingCart myCart=新建ShoppingCart();
产品[]shopProducts=新产品[]
{
新产品(“奶酪”,6,“牛奶奶酪”,“3/12/2016”,4),
新产品(“面包”,2,“谷物面包”,“2016年11月27日”,8),
新产品(“冰淇淋”,10,“冰淇淋”,“09/11/2001”,1),
新产品(“饼干”,100,“巧克力饼干”,“00/00/0000”,5),
新产品(“饼干”,0.25f,“瓦尼拉”,“08/01/2006”,6)
};
商店=新商店(商店产品);
shop.DisplayProducts();
Console.WriteLine(“-------------------------------------------------------------------------------------”);
myCart.printY=Console.CursorTop;
myCart.Display();
ConsoleKeyInfo输入;
while((input=Console.ReadKey(true)).Key!=ConsoleKey.Escape)
{
if(!Char.IsDigit(input.KeyChar))
返回;
int index=Convert.ToInt16(input.KeyChar.ToString())-1;
if(索引<1 | |索引>商店.产品.长度)
返回;
myCart.AddProduct(shop.products[index]);
商店减价金额(商店产品[指数]);
shop.DisplayProducts();
myCart.Display();
//int userInput=0;
//做
//{
//userInput=ShoppingCart();
//}while(userInput!=5);
}
}
//静态公共int ShoppingCart()
//{
//Console.WriteLine(“您的购物车”);
//Console.WriteLine();
//var result=Console.ReadLine();
//返回Convert.ToInt32(结果);
//}
}
}
购物车类
using System;
using System.Collections.Generic;
namespace SuperMarcado
{
internal class ShoppingCart
{
internal int printY;
internal List<Product> productList;
public ShoppingCart()
{
productList = new List<Product>();
}
internal void Display()
{
if (productList.Count == 0)
Console.WriteLine("------------------ EMPTY CART------------------ ");
foreach (var prod in productList)
Console.WriteLine(prod.Name + " value: " + prod.Value);
}
internal void AddProduct(Product product)
{
productList.Add(product);
}
}
}
使用系统;
使用System.Collections.Generic;
命名空间超级商场
{
内部类购物车
{
内部int-printY;
内部列表产品列表;
公共购物车()
{
productList=新列表();
}
内部空白显示()
{
如果(productList.Count==0)
Console.WriteLine(“--------------空购物车----------------”);
foreach(产品列表中的var prod)
Console.WriteLine(prod.Name+“value:”+prod.value);
}
内部产品(产品)
{
productList.Add(产品);
}
}
}
你到底有什么问题?如何添加物品,制作类似购物车的东西,并在按下数字时一直显示出来。这就是你想要它做的。你还没有说明什么地方做错了,或者什么地方没有做,而你一直坚持下去。你有没有教程?然后你需要在你的类ShoppingCart中实现你的方法Display,用一个方法来迭代AddProduct添加的产品