c# - 尝试按顺序打印链的所有节点时控制台冻结
问题描述
我已经尝试解决这个问题两天了,问了我的几个同学,没有人能找到问题,任何帮助将不胜感激,因为这个项目明天早上到期。
所以代码的说明是构建三个类: Book - 有名字和作者。作者 - 具有 book 类型的名称和节点链(作者制作的书籍列表)。图书馆 - 有两个节点链,一个是 book 类型(包含所有图书馆书籍),一个是 author 类型(包含所有在图书馆拥有书籍的作者)。
然后我们需要构建这个菜单:
1 -- Add book to library
2 -- Print all books
3 -- Print all authors
4 -- Print books of author
5 -- Get Number of Book
6 -- Exit the program
问题在于 2 - “打印所有书籍”
为了打印所有书籍,我在 Library 中创建了一个名为 PrintBooks 的函数,它基本上是 LibraryBooks 节点链的 toString():
public string PrintBooks()
{
string str = "";
Node<Book> search = LibraryBooks;
while (search != null)
{
str = str + search.GetInfo().GetName() + " " + search.GetInfo().GetAuthor() + " -> ";
search = search.GetNext();
}
return str;
}
这就是我在 Main 中使用它的方式:
static void Main(string[] args)
{
int x = 0;
Node<Book> LibraryBooks = new Node<Book>(null);
Node<Author> LibraryAuthors = new Node<Author>(null);
Library library = new Library(LibraryBooks, LibraryAuthors);
Console.WriteLine("1 -- Add book to library");
Console.WriteLine("2 -- Print all books");
Console.WriteLine("3 -- Print all authors");
Console.WriteLine("4 -- Print books of author");
Console.WriteLine("5 -- Get Number of Book");
Console.WriteLine("6 -- Exit the program");
while (x != 6) //6 -- Exit the program
{
x = int.Parse(Console.ReadLine());
if (x == 1) //1 -- Add book to librarybooks
{
string name=null, author=null;
Console.WriteLine("Please Enter Your Book's Name");
name = Console.ReadLine();
Console.WriteLine("Please Enter Your Book's Author's Name");
author = Console.ReadLine();
Book book = new Book(name, author);
library.AddBook(book);
Console.WriteLine("1 -- Add book to library");
Console.WriteLine("2 -- Print all books");
Console.WriteLine("3 -- Print all authors");
Console.WriteLine("4 -- Print books of author");
Console.WriteLine("5 -- Get Number of Book");
Console.WriteLine("6 -- Exit the program");
}
if (x == 2) //2 -- Print all books
{
string debug = "";
debug = library.PrintBooks();
Console.WriteLine(debug);
Console.WriteLine("1 -- Add book to library");
Console.WriteLine("2 -- Print all books");
Console.WriteLine("3 -- Print all authors");
Console.WriteLine("4 -- Print books of author");
Console.WriteLine("5 -- Get Number of Book");
Console.WriteLine("6 -- Exit the program");
}
此外,用于将图书添加到 Library 内的 LibraryBooks 的 AddBook 是 Library 中的一个函数:
public void AddBook(Book book)
{
if (LibraryBooks == null)
{
LibraryBooks.SetInfo(book);
LibraryBooks.SetNext(null);
}
else
{
LibraryBooks.SetNext(LibraryBooks);
LibraryBooks.SetInfo(book);
}
现在,当我尝试使用 PrintBooks 时出现错误,执行此行时控制台总是冻结:
debug = library.PrintBooks();
所以我认为问题出在 PrintBooks 上,但我的同学使用它几乎完全相同,并且对他们有用,也许这些印刷书籍实际上并没有使用与 AddBook 中的书籍添加到的相同的 LibraryBooks?正如你所看到的,这段代码并不太复杂,只是我是新手,所以我在调试不给我错误消息的错误时遇到了麻烦。
我很乐意将项目所需的任何其他部分添加到这篇文章中,以解决这个问题。
感谢您抽出时间来阅读
编辑:下面的完整代码复制错误,有很多临时部分可以忽略,我在下面上传的部分是相关的
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HelloEran
{
class Node<T>
{
private T info;
private Node<T> next;
public Node(T info)
{
this.info = info;
this.next = null;
}
public Node(T info, Node<T> next)
{
this.info = info;
this.next = next;
}
public T GetInfo()
{
return info;
}
public Node<T> GetNext()
{
return next;
}
public void SetInfo(T info)
{
this.info = info;
}
public void SetNext(Node<T> next)
{
this.next = next;
}
public int GetSize()
{
Node<T> pos = this;
int count = 0;
while (pos != null)
{
count++;
pos = pos.GetNext();
}
return count;
}
public String ToString()
{
return this.info.ToString();
}
}
class Program
{
static void Main(string[] args)
{
int x = 0;
int id = 1;
Node<Book> LibraryBooks = new Node<Book>(null);
Node<Author> LibraryAuthors = new Node<Author>(null);
Library library = new Library(LibraryBooks, LibraryAuthors);
Console.WriteLine("1 -- Add book to library");
Console.WriteLine("2 -- Print all books");
Console.WriteLine("3 -- Print all authors");
Console.WriteLine("4 -- Print books of author");
Console.WriteLine("5 -- Get Number of Book");
Console.WriteLine("6 -- Exit the program");
while (x != 6) //6 -- Exit the program
{
x = int.Parse(Console.ReadLine());
if (x == 1) //1 -- Add book to library
{
string name=null, author=null;
Console.WriteLine("Please Enter Your Book's Name");
name = Console.ReadLine();
Console.WriteLine("Please Enter Your Book's Author's Name");
author = Console.ReadLine();
Book book = new Book(name, author, id);
library.AddBook(book);
id++;
Console.WriteLine("1 -- Add book to library");
Console.WriteLine("2 -- Print all books");
Console.WriteLine("3 -- Print all authors");
Console.WriteLine("4 -- Print books of author");
Console.WriteLine("5 -- Get Number of Book");
Console.WriteLine("6 -- Exit the program");
}
if (x == 2) //2 -- Print all books
{
string debug = "";
debug = library.PrintBooks();
Console.WriteLine(debug);
Console.WriteLine("1 -- Add book to library");
Console.WriteLine("2 -- Print all books");
Console.WriteLine("3 -- Print all authors");
Console.WriteLine("4 -- Print books of author");
Console.WriteLine("5 -- Get Number of Book");
Console.WriteLine("6 -- Exit the program");
}
if (x == 3) //3-- Print all authors
{
}
if (x == 4) //4-- Print books of author
{
}
if (x == 5) //5-- Get Number of Book
{
string numname = null, numauthor = null;
Console.WriteLine("Please Enter Your Book's Name");
numname = Console.ReadLine();
Console.WriteLine("Please Enter Your Book's Author's Name");
numauthor = Console.ReadLine();
library.Idbook(numname, numauthor);
}
}
}
}
class Book
{
private string author, name;
private int id;
public Book(string name, string author,int id)
{
this.name = name;
this.author = author;
}
public string GetAuthor()
{
return author;
}
public string GetName()
{
return name;
}
public int Getid()
{
return id;
}
public void Setid(int id)
{
this.id = id;
}
public string ToString()
{
return "bookID : " + id + " , BookName : " + name + " Author'sName : " + author;
}
}
class Author
{
private string name;
private Node<Book> books;
public Author(string name, Node<Book> books)
{
this.name = name;
this.books = books;
}
public Node<Book> GetBooks()
{
return books;
}
public string PrintAuthorsBooks()
{
string str = "";
return str;
}
}
class Library
{
private Node<Book> LibraryBooks;
private Node<Author> LibraryAuthors;
public Library(Node<Book> LibraryBooks, Node<Author> LibraryAuthors)
{
this.LibraryBooks = LibraryBooks;
this.LibraryAuthors = LibraryAuthors;
}
public Node<Book> GetLibraryBooks()
{
return LibraryBooks;
}
public Node<Author> GetLibraryAuthors()
{
return LibraryAuthors;
}
public void AddBook(Book book)
{
if (LibraryBooks == null)
{
LibraryBooks.SetInfo(book);
LibraryBooks.SetNext(null);
}
else
{
LibraryBooks.SetNext(LibraryBooks);
LibraryBooks.SetInfo(book);
}
}
public void AddAuthor(Author author)
{
if (LibraryAuthors.GetInfo() == null)
{
LibraryAuthors.SetInfo(author);
LibraryAuthors.SetNext(null);
}
else
{
LibraryAuthors.SetInfo(author);
LibraryAuthors.SetNext(LibraryAuthors);
}
}
public string PrintBooks()
{
string str = "";
Node<Book> search = this.LibraryBooks;
while (search.GetInfo() != null)
{
str = str + search.GetInfo().GetName() + " " + search.GetInfo().GetAuthor() + " -> ";
search = search.GetNext();
}
return str;
}
public void Idbook(string name, string author)
{
Node<Book> temp = this.LibraryBooks;
int l = temp.GetSize();
bool exist = false;
for (int i = 0; i < l; i++)
{
if (temp.GetInfo().GetName() == name && temp.GetInfo().GetAuthor() == author)
{
exist = true;
Console.WriteLine("The ID of this book is " + temp.GetInfo().Getid());
}
temp = temp.GetNext();
}
if (exist == false)
Console.WriteLine("This book does not exist in the library");
}
}
}
解决方案
推荐阅读
- python-3.x - Pandas 按累计和分组
- reactjs - 在 Gatsby 中将 Markdown 文件导入 React 组件
- reactjs - 无法在 redux 存储中增加简单值
- amazon-web-services - 使用 s3 存储桶图像从子域加载的 Cloudfront 分发
- python - 如何单击使用 Python 嵌入的 youtube 视频的播放按钮
- go - Golang sha256 哈希不满足 okta 代码挑战
- bazel - Bazel:如何解开配置 ac/c++ 构建的 81 种方法
- javascript - webpack 热重载使用了不正确的 url
- visual-studio-code - 在哪里可以找到 VS Code 登录和同步?
- javascript - Redux - 无法使用调度在 axios 响应中调用操作