0

私は現在、クラスの作成方法とインデクサーの使用方法を学んでいます。2つのクラスlist_of_carsとを作成しましthe_carsた。Iを使用するbutton1と、リストに車を表示できますが、3台の車を表示する代わりに、2台の車しか表示しません。なぜ最後の車がないのかわかりませんか?

コード

namespace cars
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public class list_of_cars
        {
            public the_cars first;
            public the_cars last;

            public int count;

            public list_of_cars(the_cars new_car)
            {
                first = new_car;
                last = new_car;
                count = 1;
            }

            public void add_car(the_cars new_car)
            {
                if (count == 0)
                {
                    first = new_car;
                    last = new_car;
                    count = 1;
                }
                else
                {
                    last.next = new_car;
                    last = new_car;
                    count++;
                }
            }
        }

        public class the_cars
        {
            private string name;
            private int year;
            private double price;

            public the_cars next;

            public the_cars(string new_name, int new_year, double new_price)
            {
                name = new_name;
                year = new_year;
                price = new_price;

                next = null;
            }

            public override string ToString()
            {
                return name + " " + year.ToString() + " " + price.ToString();
            }
        }


        private void button2_Click(object sender, EventArgs e)
        {
            the_cars car0 = new the_cars("VW Bug", 1960, 600.0);
            the_cars car1 = new the_cars("Ford Pinto", 1970, 2400.0);
            the_cars car2 = new the_cars("Duster", 1974, 3200.0);

            list_of_cars mylist = new list_of_cars(car0);
            mylist.add_car(car1);
            mylist.add_car(car2);

            the_cars current = mylist.first;


            while (current.next != null)
            {
                textBox1.AppendText(current.ToString() + Environment.NewLine);
                current = current.next;
            }


        }
    }
}
4

2 に答える 2

3

whileループは

while(current != null)
于 2012-12-04T15:02:22.673 に答える
0

私はこれが役立つと思います:

        list_of_cars mylist = new list_of_cars();
        mylist.add_car(car0);
        mylist.add_car(car1);
        mylist.add_car(car2);
于 2012-12-04T14:59:21.453 に答える