我制作了带有列表的书架应用程序。基本上,用户输入他们想要添加的书籍数量,然后这样做。底部方法是问题的根源(ListBooks)。我正在尝试打印列表,但它将它们打印为空白。
class Shelf
{
public void Program()
{
List<string> bookTitle = new List<string>();
List<string> bookAuthor = new List<string>();
List<int> bookPageCount = new List<int>();
List<int> bookWordCount = new List<int>();
Console.WriteLine("1. Add Book.");
Console.WriteLine("2. List Book.");
Console.WriteLine("3. Quit.");
int userEntry = int.Parse(Console.ReadLine());
switch(userEntry)
{
case 1:
AddBook(bookTitle, bookAuthor, bookPageCount, bookWordCount);
break;
case 2:
ListBook(bookTitle, bookAuthor, bookPageCount, bookWordCount);
break;
case 3:
Environment.Exit(1);
break;
}
}
public void AddBook(List<string> bookTitle, List<string> bookAuthor, List<int> bookPageCount, List<int>bookWordCount)
{
string title, author;
int bookQuantity, pageCount, wordCount, userEntry;
Console.WriteLine("Enter amount of books you would like to add.");
bookQuantity = int.Parse(Console.ReadLine());
for (int x = 1; x <= bookQuantity; x++)
{
Console.WriteLine("Enter title.");
title = Console.ReadLine();
bookTitle.Add(title);
Console.WriteLine("Enter author.");
author = Console.ReadLine();
bookAuthor.Add(author);
Console.WriteLine("Enter page count.");
pageCount = int.Parse(Console.ReadLine());
bookPageCount.Add(pageCount);
Console.WriteLine("Enter word count.");
wordCount = int.Parse(Console.ReadLine());
bookWordCount.Add(wordCount);
}
Console.WriteLine("1. Main Menu.");
userEntry = int.Parse(Console.ReadLine());
switch(userEntry)
{
case 1:
Program();
break;
default:
Console.WriteLine("Please enter valid data.");
Program();
break;
}
}
public void ListBook(List<string> bookTitle, List<string> bookAuthor, List<int> bookPageCount, List<int>bookWordCount)
{
int userEntry;
Console.WriteLine("1. Titles.");
Console.WriteLine("2. Authors.");
userEntry = int.Parse(Console.ReadLine());
switch(userEntry)
{
case 1:
bookTitle.ForEach(Console.WriteLine); // Prints blank
break;
case 2:
bookAuthor.ForEach(Console.WriteLine); // Prints blank
break;
default:
Console.WriteLine("Please enter valid data.");
Program();
break;
}
Console.ReadKey();
}
}
一切似乎都在这里,所以我不知道为什么会这样做。有没有人有什么建议?提前谢谢你。
答案 0 :(得分:3)
您应该将列表保留为类字段。目前,您将它们作为局部变量,并在每次输入Program()
方法时初始化它们:
class Shelf
{
List<string> bookTitle = new List<string>();
List<string> bookAuthor = new List<string>();
List<int> bookPageCount = new List<int>();
List<int> bookWordCount = new List<int>();
public void Program()
{
// removed from here
}
}
注意:递归Program()
来电是一个坏主意。这里只会使程序逻辑变得复杂,在某些时候你会以StackOverflowException
结束,因为你会在递归调用中得到很深的回报。改为使用主菜单开关周围的循环。您应该从其他方法中删除Program()
个调用并使用无限循环:
public void Program()
{
List<string> bookTitle = new List<string>();
List<string> bookAuthor = new List<string>();
List<int> bookPageCount = new List<int>();
List<int> bookWordCount = new List<int>();
while (true)
{
Console.WriteLine("1. Add Book.");
Console.WriteLine("2. List Book.");
Console.WriteLine("3. Quit.");
int userEntry = int.Parse(Console.ReadLine());
switch (userEntry)
{
case 1:
AddBook(bookTitle, bookAuthor, bookPageCount, bookWordCount);
break;
case 2:
ListBook(bookTitle, bookAuthor, bookPageCount, bookWordCount);
break;
case 3:
Environment.Exit(1);
return;
}
}
}
如果您想进一步改进您的代码,那么创建书类,它将保存所有图书数据
public class Book
{
public string Title { get; set; }
public string Author { get; set; }
public int PageCount { get; set; }
public int WordCount { get; set; }
}
保留这些对象的单个列表,而不是四个不同的列表。