【发布时间】:2021-12-27 21:31:15
【问题描述】:
我在列表 (BLibShelf) 中有来自“书”类的这些对象,但想开始使用控制台输入将它们添加到此类列表中
namespace Library
{
class Program
{
static void Main(string[] args)
{
Book book1 = new Book("book one", "Author one", 900, 35, 16);
Book book2 = new Book("book two", "Author two", 240, 42, 8);
Book book3 = new Book("book three", "Author three", 700, 23, 8);
List<Book> BLibShelf = new List<Book>();
BLibShelf.AddRange(new List<Book>() { book1, book2, book3, book4, book5, book6, book7, book8 });
Console.ReadLine();
}
}
这是我的书课
class Book
{
public string title;
public string author;
public int pages;
public int Libcopies;
public int BCopies;
public Book(string nTitle, string nAuthor, int nPages, int nLcopies, int nBcopies)
{
title = nTitle;
author = nAuthor;
pages = nPages;
Libcopies = nLcopies;
BCopies = nBcopies;
}
【问题讨论】:
-
使用
var title = Console.ReadLine();从用户那里获取输入,然后在构造函数中使用这些变量来制作一本新书var book = new Book(title, author....);,然后使用BLibShelf.Add(book);将其添加到列表中