将多个项目添加到列表 [英] Add multiple items to a list

查看:74
本文介绍了将多个项目添加到列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

static class Program
{
    static void Main(string carMake, string carModel, string carColour, string bikeModel, string bikeMake, string bikeColour, string truckMake, string truckModel, string truckColour)
    {
        car Mynewcar = new car();
        motorbike Mynewbike = new motorbike();
        truck Mynewtruck = new truck();

        int choice = 0;
        while (choice != 5)
        {

            Console.WriteLine("MENU");
            Console.WriteLine("What service do you need");
            Console.WriteLine("1. Car");
            Console.WriteLine("2. Motorbike");
            Console.WriteLine("3. Truck");
            Console.WriteLine("4. Search");
            Console.WriteLine("5. Exit");

            choice = int.Parse(Console.ReadLine());

            switch (choice)
            {
                case 1:
                    Console.WriteLine("What is the car make?");
                    Mynewcar.make = Console.ReadLine().ToLower();
                    carMake = Console.ReadLine();
                    Console.WriteLine("");

                    Console.WriteLine("What is the car model?");
                    Mynewcar.model = Console.ReadLine().ToLower();
                    carModel = Console.ReadLine();
                    Console.WriteLine("");

                    Console.WriteLine("What is the car Colour?");
                    Mynewcar.colour = Console.ReadLine().ToLower();
                    carColour = Console.ReadLine();
                    Console.WriteLine("");

                    break;
                case 2:
                    Console.WriteLine("what is the motorbike make");
                    Mynewbike.make = Console.ReadLine().ToLower();
                    bikeMake = Console.ReadLine();
                    Console.WriteLine("");

                    Console.WriteLine("what is the motorbike model");
                    Mynewbike.model = Console.ReadLine().ToLower();
                    bikeModel = Console.ReadLine();
                    Console.WriteLine("");

                    Console.WriteLine("what is the motorbike colour");
                    Mynewbike.colour = Console.ReadLine().ToLower();
                    bikeColour = Console.ReadLine();
                    Console.WriteLine("");

                    break;
                case 3:
                    Console.WriteLine("what is the trucks make");
                    Mynewtruck.make = Console.ReadLine().ToLower();
                    truckMake = Console.ReadLine();
                    Console.WriteLine("");

                    Console.WriteLine("what is the trucks model");
                    Mynewtruck.model = Console.ReadLine().ToLower();
                    truckModel = Console.ReadLine();
                    Console.WriteLine("");

                    Console.WriteLine("what is the trucks colour");
                    Mynewtruck.colour = Console.ReadLine().ToLower();
                    truckColour = Console.ReadLine();
                    Console.WriteLine("");

                    break;
                case 4:
                    string searchchoice = "";
                    Console.WriteLine("select Car, Motobike or truck to search?");
                    searchchoice = Console.ReadLine().ToLower();
                    if (searchchoice.Equals("car"))
                    {
                        Console.WriteLine("Car in inventory: {0} - {1} - {2}", carMake, carModel, carColour);
                    }
                    else if (searchchoice.Equals("motorbike"))
                    {
                        Console.WriteLine("Motorbike in inventory: {0} - {1} - {2}", bikeMake, bikeModel, bikeColour);
                    }
                    else
                    {
                        Console.WriteLine("Trucks in inventory: {0} - {1} - {2}", truckMake, truckModel, truckColour);
                    }
                    Console.ReadLine();
                    break;
                case 5:
                    break;
                default:
                    Console.WriteLine("Sorry, invalid selection");
                    break;
            }
        }
    }

    class car
    {
        public string make { get; set; }
        public string model { get; set; }
        public string colour { get; set; }

        public List<String> carList(car Mynewcar)
        {
            List<String> caradd = new List<String>();
            caradd.Add(Mynewcar.make);
            string carMake = Mynewcar.make;

            caradd.Add(Mynewcar.model);
            string carModel = Mynewcar.model;

            caradd.Add(Mynewcar.colour);
            string carColour = Mynewcar.model;


            return caradd;
        }

    }
    class motorbike : car
    {
        public List<String> bikeList(motorbike Mynewbike)
        {
            List<String> bikeadd = new List<String>();
            bikeadd.Add(Mynewbike.model);
            string bikeModel = Mynewbike.model;

            bikeadd.Add(Mynewbike.make);
            string bikeMake = Mynewbike.make;

            bikeadd.Add(Mynewbike.colour);
            string bikeColour = Mynewbike.colour;

            return bikeadd;
        }
    }
    class truck : car
    {
        public List<String> truckList(truck Mynewtruck)
        {
            List<String> truckadd = new List<String>();
            truckadd.Add(Mynewtruck.make);
            string truckMake = Mynewtruck.make;

            truckadd.Add(Mynewtruck.model);
            string truckModel = Mynewtruck.model;

            truckadd.Add(Mynewtruck.colour);
            string truckColour = Mynewtruck.colour;

            return truckadd;
        }
    }
}

我只是想知道是否有人可以浏览一下我的代码,看看我可以改进哪些地方。

I was just wondering whether anyone could have a look through my code to see which areas I can improve on.

此外,我还在努力将不止一项添加到汽车,自行车和卡车清单中。您将如何做,以便您可以添加属性的多个实例并将其列出到控制台中?

Also I am struggling to add more than one item to the lists for car, bike and truck. How would you do it so you can add multiple instances of the properties and list them to the console?

推荐答案

代码检查:

这里没有主题,但 CodeReview上的人 a>非常乐意为您提供帮助。

This is offtopic here but the people over at CodeReview are more than happy to help you.

我强烈建议您这样做,代码中有几件事需要注意。
同样,我建议您开始阅读
教程,因为确实没有充分的理由这样做。

I strongly suggest you to do so, there are several things that need attention in your code. Likewise I suggest that you do start reading tutorials since there is really no good reason not to do so.

列表:

您自己说过:您需要一个项目清单。现在,您只存储对一项的引用。幸运的是,可以容纳一组相关的对象: 列表

As you said yourself: you need a list of items. The way it is now you only store a reference to one item. Lucky there is exactly that to hold a group of related objects: a List.

列表非常易于使用,但请查看相关内容反正文档。

Lists are very straightforward to use but take a look at the related documentation anyway.

一个非常简单的示例,将多个自行车保留在列表中:

A very simple example to keep multiple bikes in a list:

List<Motorbike> bikes = new List<Motorbike>();

bikes.add(new Bike { make = "Honda", color = "brown" });
bikes.add(new Bike { make = "Vroom", color = "red" });

要遍历列表,可以使用 foreach 语句:

And to iterate over the list you can use the foreach statement:

foreach(var bike in bikes) {
     Console.WriteLine(bike.make);
}

这篇关于将多个项目添加到列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆