列表与LT;>用户输入被覆盖

时间:2017-05-15 02:03:58

标签: c# list

好的,我正在使用自己创建的汽车类。我正在尝试使用List<Automobile>。我正在尝试编写这个程序,它将使用List<Automobile>并将用户输入存储在我的Automobile类中。当我运行我的代码并尝试在我的List<Automobile>中放置多个汽车时,它只会覆盖用户输入的上一辆汽车。我知道我的代码很乱。我完全不习惯使用List<>和写/读文件。

以防万一我的谣言不太清楚。我想弄清楚为什么List<Automobile>List<Automobile>写入多辆汽车时我的using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; namespace Exercise6_DealerVehicleInventory { class Program { static void Main(string[] args) { #region Variables/Constructors var answer = ""; Automobile car = new Automobile(); List<Automobile> vehicle = new List<Automobile>(); #endregion #region User Car Input /* I might be able to put all of this in one big while loop that way I * can have more than one vehicle wrote to this file at a time. */ Console.Write("Do you want to add a car?\nY for yes or N for no: "); answer = Console.ReadLine(); while (answer == "Y" || answer == "y") { Console.Write("\nEnter the make of the car: "); car.Make = Console.ReadLine(); Console.Write("Enter the model of the car: "); car.Model = Console.ReadLine(); Console.Write("Enter the color of the car: "); car.Color = Console.ReadLine(); Console.Write("Enter the year of the car: "); car.Year = Convert.ToInt32(Console.ReadLine()); Console.Write("Enter the mileage of the car: "); car.Mileage = Convert.ToInt32(Console.ReadLine()); vehicle.Add(car); Console.Write("Do you want to add a car?\nY for yes or N for no: "); answer = Console.ReadLine(); } #endregion #region Delete From In Memory Console.Write("\nDo you want to delete a car?\nY for yes or N for no: "); answer = Console.ReadLine(); if (answer == "Y" || answer == "y") { int i = 0; int delEntry = 0; foreach(Automobile automobile in vehicle) { Console.Write("#" + i + " = " + automobile.Make + " " + automobile.Model + " " + automobile.Color + " " + automobile.Year + " " + automobile.Mileage + "\n"); i++; } Console.WriteLine("\nWhat item number would you like to delete: "); delEntry = Convert.ToInt32(Console.ReadLine()); vehicle.RemoveAt(delEntry); } #endregion #region Write Data To File Console.Write("\nDo you want to write this to a text file?\nY for yes or N for no: "); answer = Console.ReadLine(); string mydocpath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); if (answer == "Y" || answer == "y") //(string.Equals("y", answer, StringComparison.CurrentCultureIgnoreCase)) { using (StreamWriter sw = new StreamWriter(mydocpath + @"\Vehicle.txt")) { foreach (Automobile automobile in vehicle) { sw.WriteLine(automobile.Make + " " + automobile.Model + " " + automobile.Color + " " + automobile.Year + " " + automobile.Mileage); } } } else { Console.WriteLine("No data wrote to file: "); } #endregion #region Update Vehicle File Console.Write("\nWould you like to add another vehicle?\nY for yes or N for no: "); answer = Console.ReadLine(); if (answer == "Y" || answer == "y") { StreamReader sr = new StreamReader(mydocpath + @"\Vehicle.txt"); String line = sr.ReadLine(); Console.Write("\n" + line); while (answer == "Y" || answer == "y") { Console.Write("Enter the make of the car: "); car.Make = Console.ReadLine(); Console.Write("Enter the model of the car: "); car.Model = Console.ReadLine(); Console.Write("Enter the color of the car: "); car.Color = Console.ReadLine(); Console.Write("Enter the year of the car: "); car.Year = Convert.ToInt32(Console.ReadLine()); Console.Write("Enter the mileage of the car: "); car.Mileage = Convert.ToInt32(Console.ReadLine()); vehicle.Add(car); Console.Write("\nDo you want to delete a car?\nY for yes or N for no: "); answer = Console.ReadLine(); if (answer == "Y" || answer == "y") { int i = 0; int delEntry = 0; foreach (Automobile automobile in vehicle) { Console.Write("#" + i + " = " + automobile.Make + " " + automobile.Model + " " + automobile.Color + " " + automobile.Year + " " + automobile.Mileage + "\n"); i++; } Console.WriteLine("\nWhat item number would you like to delete: "); delEntry = Convert.ToInt32(Console.ReadLine()); vehicle.RemoveAt(delEntry); } Console.Write("\nDo you want to write this to a text file?\nY for yes or N for no: "); answer = Console.ReadLine(); if (answer == "Y" || answer == "y") //(string.Equals("y", answer, StringComparison.CurrentCultureIgnoreCase)) { using (StreamWriter sw = new StreamWriter(mydocpath + @"\Vehicle.txt", true)) { foreach (Automobile automobile in vehicle) { sw.WriteLine(automobile.Make + " " + automobile.Model + " " + automobile.Color + " " + automobile.Year + " " + automobile.Mileage); } } } } } else { Console.WriteLine("Nothing else was added to the file."); } #endregion Console.ReadLine(); } } } 一直在写。

提前感谢任何人和所有人帮我解决这个问题。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Exercise6_DealerVehicleInventory
{
    class Automobile
    {
        private string _make;

        public string Make
        {
            get { return _make; }
            set { _make = value; }
        }

        private string _model;

        public string Model
        {
            get { return _model; }
            set { _model = value; }
        }

        private string _color;

        public string Color
        {
            get { return _color; }
            set { _color = value; }
        }

        private int _year;

        public int Year
        {
            get { return _year; }
            set { _year = value; }
        }

        private int _mileage;

        public int Mileage
        {
            get { return _mileage; }
            set { _mileage = value; }
        }

    }
}

以下是我的汽车课程。

scanf

2 个答案:

答案 0 :(得分:1)

可能看起来列表中的Automobile对象被覆盖,但您实际上不止一次添加相同的对象,只是覆盖了它的属性每一次。

您需要在while循环中插入此行Automobile car = new Automobile();,以便创建前一个Automobile对象的新对象独立

while (answer == "Y" || answer == "y")
{
    Automobile car = new Automobile();
    ...
    ...
    ...

答案 1 :(得分:1)

每次迭代while循环时都需要实例化一个新的Automobile,否则你只是在同一个实例上运行并覆盖它。

移动此行:

Automobile car = new Automobile();

到这里:

while (answer == "Y" || answer == "y")
{
    Automobile car = new Automobile(); // create a new Automobile each time

    Console.Write("\nEnter the make of the car: ");
    car.Make = Console.ReadLine();