2014-10-31 4 views
0

Притворись, что я прошу пользователя ввести альбом для списка вниз. У меня это прекрасно, , но по какой-то причине я не могу найти способ, чтобы они могли затем удалить введенный им вход.Как удалить пользовательский ввод, который был сохранен в 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 альбомов, но когда дело доходит до удаления альбома, я застрял. Альбом прослушивается в массиве строк. Я пробовал разные вещи в пределах моих знаний, но ничего не работает.

Спасибо всем, кто может помочь, это довольно сложно.

+0

, так что вам нужна другая функция, в которой вы будете читать название альбома и удалять его, если оно присутствует в именах альбомов, верно? –

+0

Возможно ли использовать список вместо массива? – valch

+0

Не имеет отношения к вопросу, но есть «int nextAvailableSpace = FindSlot (« »); верный? должен быть «int nextAvailableSpace = FindSlot (albumInput); –

ответ

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 функции, вы можете возвращать целое число, чтобы подтвердить удаление было успешным. Нравится:

Этот код не очень хороший, но я старался сохранить ваш стиль.

Смежные вопросы