重复打印相同的节点部分,有助于构建C#代码



我不是一个很老练的程序员,作为家庭工作,我得到了一个项目,在这个项目中,我需要构建一个据称有助于管理库之类的代码。

问题是,我已经为一个将以node<T>格式存储的书籍列表创建了一个类,但是,在我构建了将一本书添加到列表的函数之后,我在本应打印所有书籍的函数"printchain"中做了一些错误的事情,使它一次又一次地重复打印同一本"书"。

为什么它不能正确打印书籍的node<T>?请主要参考"图书馆图书"课程和主课程,因为这些是我在这里处理这个问题的地方。

namespace ConsoleApp7
{
public class book
{
private int id;
private string authorname;
private string name;
public book(int id, string authorname, string name)
{
this.id = id;
this.name = name;
this.authorname = authorname;
}

public string getname()
{
return this.name;
}
public string getauthor()
{
return this.authorname;
}
public void setname(string name)
{
this.name = name;
}
public void setauthor(string authorname)
{
this.authorname = authorname;
}
public int getid()
{
return this.id;
}
public void setid(int id)
{
this.id = id;
}
}
public class author
{
private string name;
private librarybooks autbooks;
public author(string name, librarybooks autbooks)
{
this.name = name;
this.autbooks = autbooks;
}
public string getname()
{
return this.name;
}
public void setname(string name)
{
this.name = name;
}
public librarybooks getauthorsbooks()
{
return this.autbooks;
}
public void setauthorsbooks(librarybooks autbooks)
{
this.autbooks = autbooks;
}
}
public class librarybooks
{
private node<book> books;
public librarybooks()
{
this.books = null;
}
public node <book> Getbooks() { return books; }
public void add(book book)
{
this.books = new node <book>(book, this.books);
}
public void printchain()
{
while (this.books.getNext().getValue() != null)
{
node<book> search = this.books;
Console.WriteLine(search.getValue().getauthor() + " " + search.getValue().getid() + " " + search.getValue().getname());
search = new node<book>(this.books.getNext().getValue(), this.books) ;
}
}
}
public class libraryauthors
{
private node<author> authors;
public libraryauthors()
{
this.authors = null;
}
public void add(author author)
{
if (this.authors.getValue() == null)
{
this.authors.setValue(author);
this.authors.setNext(null);
}
if (this.authors.getValue() != null)
{
this.authors.setNext(this.authors);
this.authors.setValue(author);
}
}
}
public class node<T>
{
T value;
node<T> next;
public node(T x)
{
this.value = x;
this.next = null;
}
public node(T x, node<T> next)
{
this.value = x;
this.next = next;
}
public T getValue()
{
return this.value;
}
public void setValue(T value)
{
this.value = value;
}
public node<T> getNext()
{
return this.next;
}
public void setNext(node<T> next)
{
this.next = next;
}
}
class Program
{
static void Main(string[] args)
{
librarybooks ezer = new librarybooks();
string str1, str2;
int num1;
Console.WriteLine("enter author");
str1 = Console.ReadLine();
Console.WriteLine("enter name");
str2 = Console.ReadLine();
Console.WriteLine("enter id");
num1 = int.Parse(Console.ReadLine());
book ezra=new book(num1,str2,str1);
string str3, str4;
int num2;
Console.WriteLine("enter author");
str3 = Console.ReadLine();
Console.WriteLine("enter name");
str4 = Console.ReadLine();
Console.WriteLine("enter id");
num2 = int.Parse(Console.ReadLine());
book ezra2 = new book(num2, str4, str3);
ezer.add(ezra2);
ezer.add(ezra);
ezer.printchain();
}
}
}

您的循环当前如下所示:

while (this.books.getNext().getValue() != null)
{
node<book> search = this.books;
Console.WriteLine(...);
search = new node<book>(this.books.getNext().getValue(), this.books) ;
}

看起来您正试图在列表中进行迭代。但是:

  • 您一直在实例化search,但实际上并没有在任何地方使用它
  • 你真的没有一个变量来跟踪"我在列表中的位置">

另一种方法可能是使用List<book>。默认情况下,它附带了C#,然后您可以使用foreach(book b : this.books)循环迭代您的List。它将使您不必实现自己的node<T>类。

最新更新