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

时间:2014-10-31 09:03:59

标签: c# arrays variables user-input

假装我要求用户输入要列出的专辑。 我很好, 但由于某种原因,我无法找到一种方法让他们能够删除他们所做的输入。

这是我用来存储他们拥有的任何专辑输入的方法

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张专辑,但是在删除专辑时我会被卡住。 专辑是在一个字符串数组中听。在我的知识范围内,我尝试了各种各样的东西,但似乎没有任何工作。

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

1 个答案:

答案 0 :(得分:0)

您始终将""发送至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();
}

这段代码不好,但我试着保持你的风格。