将“double”传递给“book”类型方法

时间:2015-02-11 02:27:58

标签: c# arrays

我正在尝试将“double”类型传递给“book”类型的方法。不知道从哪里开始。

Console.WriteLine("Enter the item for append");
                item = double.Parse(Console.ReadLine());
                recbook.append(item);
                Console.WriteLine("The items in the array");
                recbook.display();

方法

 public void append(book Item)
    {
        arr[arraySize] = Item;
        arraySize = arraySize + 1;
    }

课本在

    public class unsortedArrayAccess
{
    public static book[] arr;
    int arraySize;
    public struct book
    {
        public int id;
        public string title;
        public double price;
    }




    public unsortedArrayAccess(int scale)
    {
        arr = new book[scale];
        arraySize = 0;

        for (int i = 0; i < scale; i++)
        {
            Console.WriteLine("Enter an id");
            arr[i].id = int.Parse(Console.ReadLine());


            Console.WriteLine("Enter a book title:");
            arr[i].title = Console.ReadLine();

            Console.WriteLine("Enter a price:");
            arr[i].price = double.Parse(Console.ReadLine());

        }
    }

    public book get(int i)
    {
        return arr[i];
    }

    public int search(int Key)
    {
        int i = 0;
        while ((arr[i].id != Key) && (i < arraySize))
            i = i + 1;
        if (i < arraySize) return i;
        else
        {
            Console.WriteLine("There is no such item!");
            return -1;
        };
    }

    public void append(book Item)
    {
        arr[arraySize] = Item;
        arraySize = arraySize + 1;
    }

    public int remove()
    {
        if (arraySize == 0)
        {
            Console.WriteLine("There is no item in the array!");
            return -1;
        }
        book x = arr[arraySize - 1];
        arraySize = arraySize - 1;
        return 1;
    }

    public void deletion(int Key)
    {
        int k = search(Key);
        if (k != -1)
        {
            for (int i = k; i < arraySize; i++)
                arr[i] = arr[i + 1];
            arraySize = arraySize - 1;
        };
    }

    public void display()
    {
        if (arraySize == 0)
            Console.WriteLine("Array is empty!");
        else
        {
            for (int i = 0; i < arraySize; i++)
                Console.WriteLine(arr[i]);
        };

        Console.WriteLine("array size is " + arraySize);
    }
}

1 个答案:

答案 0 :(得分:0)

我不确切地知道你想要实现什么,但使用结构和数组使事情复杂化。为什么不这样做一个简单的实现:

public class Book
{
    public  int Id { get; set; }
    public string Title { get; set; }
    public double Price { get; set; }
}

public void TestBook()
{
    var books= new List<Book>();

    // to add a book
    books.Add(new Book { Id = 1, Title = "War and Peace", Price = 30.12});
    books.Add(new Book { Id = 2, Title = "Learn C# in 60 seconds", Price = 20.73 });
    books.Add(new Book { Id = 3, Title = "The Bible", Price = 10.56 });
    books.Add(new Book { Id = 4, Title = "How to become rich", Price = 44.12 });

    // to remove a book using an index
    // note the list index starts at 0 base, deletes Learn c#.... book
    books.RemoveAt(1); 

    // to remove a book using an instance
    var book = books.Find(b => b.Id.Equals(4));
    books.Remove(book);

    // to display all books titles
    books.ForEach(b => Console.WriteLine(b.Title));
}

我希望这能让你更好地使用C#。