如何使用ReadLine读取多行

时间:2017-04-09 03:06:43

标签: c#

我有这个代码,我从中获取用户的数据来创建车辆对象。唯一的问题是,因为它们是我要求的不同数据类型,我必须在ReadReadLine之间切换。当代码到达ReadLine部分时,它会打印出所有WriteLine语句,而不是只等待第一个语句的输入。

代码:

Console.Write("What would you like to do? ");
s = Console.ReadLine();
if (s == "1")
{
    Console.Write("Please Enter the vehicle ID: \n");
    ID = Console.Read();
    Console.Write("Please enter the vehicle make: \n");
    make = Console.ReadLine();
    Console.Write("Please enter the vehicle model: \n");
    model = Console.ReadLine();

    Console.Write("Please enter the vehicle year: \n");
    year = Console.Read();

    Vehicle v;
    v = new Vehicle
    {
        Id = ID,
        Make = make,
        Model = model,
        Year = year
    };

    Create(v);
}

输出:

 Please enter the vehicle ID:
 Input: 123

 Please enter the Vehicle make:

 Please enter the Vehicle model:

这是我的课程定义,如评论中所要求的那样:

public class Vehicle
{
    public int Id { get; set; }
    public int Year { get; set; }
    public string Make { get; set; }
    public string Model { get; set; }
    public static string Class { get; set; }

    public interface VehicleService
    {
        List<Vehicle> Get();
        Vehicle Get(int id);
        void Create(Vehicle vehicle);
        void Update(Vehicle vehicle);
        void Delete(Vehicle vehicle);
    }
}

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:0)

看来你刚刚开始学习编码。以下是您作为新手需要做的事情。尝试花时间来理解下面的代码并尝试改进。你还没有显示对象成员的数据类型,所以我做了一个假设。

static void Test1()
{
    string s = "", make = "", model = "";
    int ID, year;

    Console.Write("What would you like to do ? Enter option between 1 or 2 : ");
    //Console.ReadLine() will take the all the characters typed by the user before enter key is hit.
    //These characters will be assigned to a string s.
    //If you want to read next character use Console.Read(). 
    //Note: Its returns an int rather than byte or string.
    //since your if condition matches it with "1", you can use ReadLine()

    s = Console.ReadLine();
    if (s == "1")
    {
        string consoleLine;
        bool validInput = false;

        //you can create a function for the validation and re-enter
        do
        {
            Console.WriteLine("Enter the vehicle ID: ");
            consoleLine = Console.ReadLine();
            //check for validity
            if (int.TryParse(consoleLine, out ID))
            {
                //assuming ID should be between 1 and 10
                if (ID > 0 && ID < 11)
                {
                    validInput = true;
                }
                else
                    Console.Write("Vehicle ID should be between 1 and 10. Re-");
            }
            else
            {
                Console.WriteLine("Vehicle ID should be an integer. Re-");
            }
        } while (!validInput);

        validInput = false;
        do
        {
            Console.WriteLine("Enter the Year: ");
            consoleLine = Console.ReadLine();
            //check for validity
            if (int.TryParse(consoleLine, out year))
            {
                //assuming ID should be between 1 and 10
                if (year > 0 && year < 2021)
                {
                    validInput = true;
                }
                else
                    Console.Write("Year should be between 1 and 2020. Re-");
            }
            else
            {
                Console.WriteLine("Year should be an integer. Re-");
            }
        } while (!validInput);

        validInput = false;
        do
        {
            Console.WriteLine("Enter the vehicle make: ");
            make = Console.ReadLine();

            if (make.Length > 0)
            {
                validInput = true;
            }
            else
                Console.Write("Vehicle Make should be a valid text. Re-");
        } while (!validInput);

        validInput = false;
        do
        {
            Console.WriteLine("Enter the vehicle model: ");
            model = Console.ReadLine();

            if (model.Length > 0)
            {
                validInput = true;
            }
            else
                Console.Write("Vehicle Model should be a valid text. Re-");
        } while (!validInput);


        //here you have received all input
        //now assign to Vehicle
        var vehicle = new Vehicle
        {
            Id = ID,
            Make = make,
            Year = year,
            Model = model
        };

        //Below code shows you that values are stored in the object vehicle
        Console.WriteLine("Vehicle details stored in the object vehicle");
        Console.WriteLine("Vehicle Id = " + vehicle.Id.ToString());
        Console.WriteLine("Vehicle Year = " + vehicle.Year.ToString());
        Console.WriteLine("Vehicle Make = " + vehicle.Make);
        Console.WriteLine("Vehicle Model = " + vehicle.Model);

        //You have not shown where you have declared the List<Vehicle> to
        //store the collection of Vehicles.
        //You might have implemented VehicleService. So you need to search the 
        //Id in the List of Vehicle and if found use Update() else use Create()
        //through the service implementation.
    }

    Console.WriteLine("Done. Press enter to Exit.");
    Console.ReadKey();
}

答案 1 :(得分:0)

You should use Console.ReadLine(); instead of Console.Read();, you can find different of them by difference-between-console-read-and-console-readline

If you still want to use Console.Read(); please use FlushKeyboard() method.

private static void FlushKeyboard()
{
    while (Console.In.Peek() != -1)
        Console.In.Read();
}

And use this method after calling Console.Read();

Console.Write("What would you like to do? ");
s = Console.ReadLine();
if (s == "1")
{
    Console.Write("Please Enter the vehicle ID: \n");
    ID = Console.Read();
    FlushKeyboard();
    Console.Write("Please enter the vehicle make: \n");
    make = Console.ReadLine();
    Console.Write("Please enter the vehicle model: \n");
    model = Console.ReadLine();

    Console.Write("Please enter the vehicle year: \n");
    year = Console.Read();
    FlushKeyboard();

    Vehicle v;
    v = new Vehicle
    {
        Id = ID,
        Make = make,
        Model = model,
        Year = year
    };
    Create(v);
}