如何删除已存储在C#中的用户输入



假设我要求用户输入要列出的相册。我有这个罚款,但由于某种原因,我无法找到一种方法让他们能够删除他们所做的输入。

这是我用来存储他们有的任何相册输入的方法

static void InsertNewAlbum()
{
    //Variable for user input
    string albumInput
    //Ask for the user for details 
    Console.WriteLine("Enter the Title of the album you would like to store");
    albumInput = Console.ReadLine();
    //Process the input of the user to make it easier to search later on
    albumInput = albumInput.ToUpper();
    albumInput = albumInput.Trim();
    //Find an empty spot within list
    int nextAvailableSpace = FindSlot("");
    if (nextAvailableSpace != -1)
    {
        //Put customer information within an empty slot into the car park
        albumNames[nextAvailableSpace] = albumInput;
    }
    else
    {
        //Inform that the usercannot park as the parking space is full
        Console.WriteLine("Sorry, but there are no available spaces left to store your       album.");
        Console.ReadLine();
    }
}

以下是"FindSlot"方法的运行方式,对于那些好奇的人来说

static int FindSlot(string albumName)
//Finding an empty slot for the user to put their car in
{
    int result = - 1;
    for (int index = 0; index < albumNames.Length; index++)
    {
        if (albumNames[index] == albumNames)
        {
            result = index;
            break;
        }
        else if (albumName == "" && albumNames[index] == null)
        {
            result = index;
            break;
        }
    }
    return result;
}   

albumNames是它放入客户放入的相册中的字符串,它是一个静态字符串。

所以,是的,用户可以自由放入10个相册,但当涉及到删除相册时,我会陷入困境。这张专辑是在一个字符串数组中收听的。在我所知的范围内,我尝试了各种各样的方法,但似乎都不起作用。

感谢任何能提供帮助的人,这很复杂。

您总是将""发送到FindSlot,就像您应该发送albumInput一样。如果你需要知道相册是否存在,如果你的输入存在,首先控制所有的albumNames,然后寻找空间。类似:

static int FindSlot(string albumName)
//Finding an empty slot for the user to put their car in
{
    for (int index = 0; index < albumNames.Length; index++)
    {
        if (albumNames[index] == albumName)
        {
            return index;
        }
    }
    for (int index = 0; index < albumNames.Length; index++)
    {
        if (albumNames[index] == "")
        {
            albumNames[index] = albumName;
            return index;
        }
    }
    return -1;
}

并称之为:

static void InsertNewAlbum()
{
    //Variable for user input
    string albumInput;
    //Ask for the user for details 
    Console.WriteLine("Enter the Title of the album you would like to store");
    albumInput = Console.ReadLine();
    //Process the input of the user to make it easier to search later on
    albumInput = albumInput.ToUpper();
    albumInput = albumInput.Trim();
    //Find an empty spot within list
    int nextAvailableSpace = FindSlot(albumInput);
    if (nextAvailableSpace != -1)
    {
        Console.WriteLine(albumInput + " is stored successfully at slot" + nexAvailableSpace + "");
    }
    else
    {
        //Inform that the usercannot park as the parking space is full
        Console.WriteLine("Sorry, but there are no available spaces left to store your       album.");
    }
    Console.ReadLine();
}

删除;你可以使用类似的功能:

static int DeleteAlbum(string albumName)
//Finding an empty slot for the user to put their car in
{
    for (int index = 0; index < albumNames.Length; index++)
    {
        if (albumNames[index] == albumName)
        {
            albumNames[index] = "";
            return index;
        }
    }
    return -1;
}

作为FindSlot函数,您可以返回一个整数来确认删除成功。类似:

static void DeleteAnAlbum()
{
    //Variable for user input
    string albumInput;
    //Ask for the user for details 
    Console.WriteLine("Enter the Title of the album you would like to store");
    albumInput = Console.ReadLine();
    //Process the input of the user to make it easier to search later on
    albumInput = albumInput.ToUpper();
    albumInput = albumInput.Trim();
    //Find and delete the album from the List
    int deletedIndex = DeleteAlbum(albumInput);
    if (deletedIndex != -1)
    {
        Console.WriteLine(albumInput + " is deleted successfully");    
        }
    else
    {
        //Inform that the usercannot delete
        Console.WriteLine("Sorry, but there are no albums with given name.");
    }
        Console.ReadLine();
}

这个代码不好,但我尽量保持你的风格。

相关内容

  • 没有找到相关文章

最新更新