自定义List的CMD菜单开发:指定位置插入文本功能问题求助
Hey there! Let's work through that issue you're having with your custom List's insert functionality in the CMD menu. From the code snippet you shared, I can spot a few common pitfalls that might be causing problems—let's break them down and fix things step by step.
1. Add Input Validation for the Position
Right now you're using int.Parse() directly, which will throw an unhandled error if the user enters something that isn't a number. Let's replace that with int.TryParse() to handle invalid input gracefully:
Console.WriteLine("Type the position where you want to paste the text: "); if (!int.TryParse(Console.ReadLine(), out int position)) { Console.WriteLine("Oops! That's not a valid number. Please try again."); return; // Or loop back to the menu if you're in a repeating menu loop }
2. Validate the Position Range
Custom List implementations usually require insert positions to be between 0 and Count (inclusive—since inserting at Count adds the item to the end). Add a check to make sure the user's input falls in this valid range:
if (position < 0 || position > myCustomList.Count) { Console.WriteLine($"Whoops! Valid positions are from 0 to {myCustomList.Count}."); return; }
3. Ensure Your Custom Insert Method is Implemented Correctly
If your custom List's Insert method has bugs, even correct menu code won't work. Here's a standard, working implementation for reference (adjust based on your List's internal structure):
public class CustomList<T> { private T[] _items; public int Count { get; private set; } public CustomList() { _items = new T[4]; // Initial default capacity Count = 0; } public void Insert(int index, T item) { // Validate index first (matches the check in your menu) if (index < 0 || index > Count) throw new ArgumentOutOfRangeException(nameof(index), "Index is out of valid range."); // Resize internal array if it's full if (Count == _items.Length) ResizeArray(_items.Length * 2); // Shift elements from index onwards to the right by one Array.Copy(_items, index, _items, index + 1, Count - index); // Insert the new item _items[index] = item; Count++; } private void ResizeArray(int newSize) { T[] newItems = new T[newSize]; Array.Copy(_items, newItems, Count); _items = newItems; } }
4. Full Working Code for Menu Option 2
Putting it all together, here's how your menu option should look:
else if (selectedMenuItem == "2. Press here to insert any text on any position.") { Console.WriteLine("Type any text which you want to Insert: "); string input = Console.ReadLine(); // Handle empty or whitespace-only input if (string.IsNullOrWhiteSpace(input)) { Console.WriteLine("Text cannot be empty or just whitespace!"); return; } Console.WriteLine("Type the position where you want to paste the text: "); if (!int.TryParse(Console.ReadLine(), out int position)) { Console.WriteLine("Oops! That's not a valid number. Please try again."); return; } if (position < 0 || position > myCustomList.Count) { Console.WriteLine($"Whoops! Valid positions are from 0 to {myCustomList.Count}."); return; } try { myCustomList.Insert(position, input); Console.WriteLine($"Successfully inserted '{input}' at position {position}!"); } catch (Exception ex) { Console.WriteLine($"Insert failed: {ex.Message}"); } }
If you're still hitting issues, double-check if your custom List's internal array is being resized correctly, or if the element shifting logic in Insert is off. Small bugs there can cause weird behavior like missing items or index errors!
内容的提问来源于stack exchange,提问作者user8814630

