问题描述:

Pretend that I'm asking the user to input an album to list down.

I have this fine,

but for some reason I cant figure out a way for them to be able to then delete the input that they have made.

This is the method that I have used to store any album input that they have

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();

}

}

Here's the how the "FindSlot" method goes, for anyone that curious

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 is the string that it puts in album the customer puts in and it's a static string.

So yeah, the user can freely put in 10 albums, but when it comes to deleting an album I get stuck.

The album is listen within a string array. I've tried various things within my knowledge but nothing seems to be working.

Thanks for anyone that can help, it is quite complicated.

网友答案:

You always send "" to FindSlot, as you should be sending the albumInput. If you need to know if the album is present, first control all the albumNames if your input is present, then look for a space. Like:

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;
}

And call it like:

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();
}

To delete; you can use a similar function:

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;
}

As your FindSlot function, you can return an integer to confirm deletion was successful. Like:

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();
}

This code is not good, but I tried to keep your style.

相关阅读:
Top