C#删除库存
您好,抱歉,如果这是一个愚蠢的问题,我对编码(使用C#)是新手,所以这就是为什么,我在做这个库存atm,我试图找出如何在选择该选项后删除库存,它只有一个字符串,不使用列表。 我有什么事吗?也许是,但我迷路了C#删除库存,c#,C#,您好,抱歉,如果这是一个愚蠢的问题,我对编码(使用C#)是新手,所以这就是为什么,我在做这个库存atm,我试图找出如何在选择该选项后删除库存,它只有一个字符串,不使用列表。 我有什么事吗?也许是,但我迷路了 static void Main(string[] args) { var meny = true; var item = new List<string>(); string invento
static void Main(string[] args)
{
var meny = true;
var item = new List<string>();
string inventory = "Nothing stored";
while (meny)
{
Console.WriteLine(" \tWelcome to the inventory");
Console.WriteLine("\t[1] Add item");
Console.WriteLine("\t[2] Show your inventory");
Console.WriteLine("\t[3] Delete your inventory");
Console.WriteLine("\t[4] Quit");
Console.Write("\tChoose: ");
int menyVal = Convert.ToInt32(Console.ReadLine());
// switch
switch (menyVal)
{
case 1:
Console.WriteLine("\nAdd item: ");
inventory =(Console.ReadLine());
break;
case 2:
Console.WriteLine(inventory);
break;
case 3:
Console.WriteLine("\n\tRemoving items ");
break;
case 4:
meny = false;
break;
default:
Console.WriteLine("\nError, Not a valid value, Try again");
break;
static void Main(字符串[]args)
{
var meny=真;
var item=新列表();
string inventory=“未存储任何内容”;
while(meny)
{
Console.WriteLine(“\tWelcome to the inventory”);
Console.WriteLine(“\t[1]添加项”);
Console.WriteLine(“\t[2]显示您的清单”);
Console.WriteLine(“\t[3]删除您的库存”);
Console.WriteLine(“\t[4]退出”);
控制台。写入(“\t选择:”);
int menyVal=Convert.ToInt32(Console.ReadLine());
//开关
开关(menyVal)
{
案例1:
Console.WriteLine(“\n添加项:”);
inventory=(Console.ReadLine());
打破
案例2:
控制台写入线(库存);
打破
案例3:
Console.WriteLine(“\n\t移动项目”);
打破
案例4:
meny=false;
打破
违约:
Console.WriteLine(“\n错误,不是有效值,请重试”);
打破
编辑:
首先,感谢大家这么快,真的很有帮助。我真的很感激!在这个案例或项目中,只需要在库存中存储1个项目,并在案例3中删除该项目。我在这里的一条评论中发现,我只需要使用inventory=null;项目就可以像它假定的那样工作。我还完全删除了该行var items=new List();因为当我不使用列表atm:D时不需要它
但再次感谢大家!D请参阅内联评论
static void Main(string[] args)
{
var meny = true;
var items = new List<string>();
string newInventoryItem = "";
while (meny)
{
Console.WriteLine(" \tWelcome to the inventory");
Console.WriteLine("\t[1] Add item");
Console.WriteLine("\t[2] Show your inventory");
Console.WriteLine("\t[3] Delete your inventory");
Console.WriteLine("\t[4] Quit");
Console.Write("\tChoose: ");
int menyVal = Convert.ToInt32(Console.ReadLine());
// switch
switch (menyVal)
{
case 1:
Console.WriteLine("\nAdd item: ");
// Read the new item from the console
newInventoryItem = Console.ReadLine();
// Add it to our list of inventory items.
items.Add(newInventoryItem);
break;
case 2:
//take our items, and put together a string
// where each item is on a new line
var itemStr = string.Join("\r\n", items);
Console.WriteLine(itemStr);
break;
case 3:
Console.WriteLine("\n\tRemoving items ");
// Remove everything from the list
items.Clear();
break;
case 4:
meny = false;
break;
default:
Console.WriteLine("\nError, Not a valid value, Try again");
break;
}
static void Main(字符串[]args)
{
var meny=真;
var items=新列表();
字符串newInventoryItem=“”;
while(meny)
{
Console.WriteLine(“\tWelcome to the inventory”);
Console.WriteLine(“\t[1]添加项”);
Console.WriteLine(“\t[2]显示您的清单”);
Console.WriteLine(“\t[3]删除您的库存”);
Console.WriteLine(“\t[4]退出”);
控制台。写入(“\t选择:”);
int menyVal=Convert.ToInt32(Console.ReadLine());
//开关
开关(menyVal)
{
案例1:
Console.WriteLine(“\n添加项:”);
//从控制台读取新项目
newInventoryItem=Console.ReadLine();
//将其添加到我们的库存项目列表中。
项目。添加(新的库存项目);
打破
案例2:
//拿着我们的东西,把一根绳子放在一起
//其中每个项目都位于新行上
var itemStr=string.Join(“\r\n”,items);
控制台写入线(itemStr);
打破
案例3:
Console.WriteLine(“\n\t移动项目”);
//从列表中删除所有内容
items.Clear();
打破
案例4:
meny=false;
打破
违约:
Console.WriteLine(“\n错误,不是有效值,请重试”);
打破
}
请参阅内联注释
static void Main(string[] args)
{
var meny = true;
var items = new List<string>();
string newInventoryItem = "";
while (meny)
{
Console.WriteLine(" \tWelcome to the inventory");
Console.WriteLine("\t[1] Add item");
Console.WriteLine("\t[2] Show your inventory");
Console.WriteLine("\t[3] Delete your inventory");
Console.WriteLine("\t[4] Quit");
Console.Write("\tChoose: ");
int menyVal = Convert.ToInt32(Console.ReadLine());
// switch
switch (menyVal)
{
case 1:
Console.WriteLine("\nAdd item: ");
// Read the new item from the console
newInventoryItem = Console.ReadLine();
// Add it to our list of inventory items.
items.Add(newInventoryItem);
break;
case 2:
//take our items, and put together a string
// where each item is on a new line
var itemStr = string.Join("\r\n", items);
Console.WriteLine(itemStr);
break;
case 3:
Console.WriteLine("\n\tRemoving items ");
// Remove everything from the list
items.Clear();
break;
case 4:
meny = false;
break;
default:
Console.WriteLine("\nError, Not a valid value, Try again");
break;
}
static void Main(字符串[]args)
{
var meny=真;
var items=新列表();
字符串newInventoryItem=“”;
while(meny)
{
Console.WriteLine(“\tWelcome to the inventory”);
Console.WriteLine(“\t[1]添加项”);
Console.WriteLine(“\t[2]显示您的清单”);
Console.WriteLine(“\t[3]删除您的库存”);
Console.WriteLine(“\t[4]退出”);
控制台。写入(“\t选择:”);
int menyVal=Convert.ToInt32(Console.ReadLine());
//开关
开关(menyVal)
{
案例1:
Console.WriteLine(“\n添加项:”);
//从控制台读取新项目
newInventoryItem=Console.ReadLine();
//将其添加到我们的库存项目列表中。
项目。添加(新的库存项目);
打破
案例2:
//拿着我们的东西,把一根绳子放在一起
//其中每个项目都位于新行上
var itemStr=string.Join(“\r\n”,items);
控制台写入线(itemStr);
打破
案例3:
Console.WriteLine(“\n\t移动项目”);
//从列表中删除所有内容
items.Clear();
打破
案例4:
meny=false;
打破
违约:
Console.WriteLine(“\n错误,不是有效值,请重试”);
打破
}
首先查看内联注释,但是,您没有将任何项目添加到列表中。您可以使用.Add
方法执行此操作
然后,当您想要删除库存中的某个项目时,您需要检查要在该列表中删除的索引
using System;
using System.Collections.Generic;
using System.Linq;
...
public static List<string> inventory = new List<string>();
public static void printInventory()
{
Console.WriteLine("===Your Inventory===");
foreach(var item in Enumerable.Range(0,inventory.Count)) // Loop through inventory to get all the items.
{
Console.WriteLine(item+": "+inventory[item]);
}
}
static void Main(string[] args)
{
var meny = true;
while (meny)
{
Console.WriteLine(" \tWelcome to the inventory");
Console.WriteLine("\t[1] Add item");
Console.WriteLine("\t[2] Show your inventory");
Console.WriteLine("\t[3] Delete your inventory");
Console.WriteLine("\t[4] Quit");
Console.Write("\tChoose: ");
int menyVal = Convert.ToInt32(Console.ReadLine());
// switch
switch (menyVal)
{
case 1:
Console.WriteLine("\nAdd item: ");
inventory.Add(Console.ReadLine()); //Add items to your inventory
break;
case 2:
printInventory(); // Print all the items in your inventory
break;
case 3:
printInventory(); // Print all the items in your inventory
Console.WriteLine("\nSelect what Item you want to delete");
// If you want to delete the entire inventory...
// just use inventory.Clear(); here instead of what's below.
try
{
int entry = int.Parse(Console.ReadLine()); // Take input selection
inventory.RemoveAt(entry); // Remove selected entry in inventory
}
catch(FormatException ex)
{
Console.WriteLine("Incorrect input, please use numbers.", ex);
}
break;
case 4:
meny = false;
break;
default:
Console.WriteLine("\nError, Not a valid value, Try again");
break;
}
}
Console.ReadLine();
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
...
公共静态列表库存=新列表();
公共静态目录()
{
Console.WriteLine(“==您的清单==”);
foreach(Enumerable.Range(0,inventory.Count)中的var项)//循环遍历库存以获取所有项。
{
Console.WriteLine(项目+”:“+库存[项目]);
}
}
静态void Main(字符串[]参数)
{
var meny=真;
while(meny)
{
Console.WriteLine(“\tWelcome to the inventory”);
有限公司