问题
I am trying to make a bookshelf application but I am having trouble with using lists. What I am hoping for is after the user specifies how many books they would like to add, the for-loop should hopefully repeat the method in the specified amount.
After the first run through of the method, the more titles added will add onto the list.
class Shelf
{
public void Program()
{
Book book = new Book();
int bookAmount;
Console.WriteLine("How many books are you adding.");
bookAmount = int.Parse(Console.ReadLine());
for(int x = 0; x <= bookAmount; x++)
{
AddBook(book);
}
}
public void AddBook(Book book)
{
List<string> bookTitles = new List<string>();
string bookTitle;
Console.WriteLine("Enter title.");
bookTitle = Console.ReadLine();
bookTitles.Add(bookTitle);
bookTitles = book.Title; // 'Cannot implicitly convert type 'string' to 'System.Collections.Generic.List<string>'
}
}
class Book
{
private string title;
public string Title
{
get { return title; }
set { title = value; }
}
}
Any critique is welcome. Thank you in advance.
回答1:
What you would want to do would be something like this:
public class Book
{
public string ISBN { get; set; }
public string Title {get; set; }
public string Author {get; set; }
}
The book will represent our data model. Now you would do this:
List<Book> library = new List<Book>();
int quantity;
while(quantity < 7)
{
library.Add("12345", "C#", "Someone");
}
What the code is doing, we create a List<Book> which will hold our data model. Then you would have a loop that iterates based on value the user inputs. Then you would simply call the library (List) and add after it ask the user for input.
Obviously I'm not attempting to get user input or validation, but is using the example of how to use a List.
回答2:
Following can be helpful:
class Shelf
{
List<Book> books = new ArrayList<Book>();
public void Program()
{
int numberOfBooks;
Console.WriteLine("How many books are you adding.");
numberOfBooks = int.Parse(Console.ReadLine());
for(int x = 0; x <= numberOfBooks; x++)
{
AddBook();
}
}
public void AddBook()
{
string bookTitle;
Console.WriteLine("Enter title.");
bookTitle = Console.ReadLine();
books.Add(new Book(bookTitle));
}
}
class Book
{
private string title;
public Book(string _title)
{
title = _title;
}
public string Title
{
get { return title; }
set { title = value; }
}
}
Keep in mind to:
- Assign meaningful names to variables
- Use input arguments to methods when it is needed
- Pay more attention to Global and Local variables and when they should be used
回答3:
I think that you must change your code like below code:
1- you must create book in addbook but you crate class in main program function
2- you can not convert string to list
3- your colcetion must contain a book
class Shelf
{
public void Program()
{
int bookAmount;
Console.WriteLine("How many books are you adding.");
bookAmount = int.Parse(Console.ReadLine());
//book is deleted
for(int x = 0; x <= bookAmount; x++)
{
AddBook();//parameter was deleted and aff in function
}
}
List<Book> bookTitles = new List<Book>(); //collection create generally in class like fields
public void AddBook()
{
Book book = new Book();
string bookTitle;
Console.WriteLine("Enter title.");
Book.Title = Console.ReadLine();
bookTitles.Add(book); //add book to colection
}
}
class Book
{
private string title;
public string Title
{
get { return title; }
set { title = value; }
}
}
来源:https://stackoverflow.com/questions/25333212/trying-to-make-a-bookshelf-console-application-but-having-some-trouble-with-list