我有這個代碼,我從中獲取用戶數據以創建車輛對象。唯一的問題是,因爲它們是不同的數據類型,我要求我必須在Read
和ReadLine
之間切換。當代碼到達ReadLine
部分時,它會打印出我的所有WriteLine
語句,而不是隻等待第一個輸入。如何使用ReadLine讀取多行文件
代碼:
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);
}
}
任何幫助將不勝感激。
取出'\ N'在'寫後使用此方法('也許吧。 –
你應該只使用'ReadLine'和解析結果的類型你需要像'ID = int.Parse(Console.ReadLine());''Read''只是從控制檯讀取一個字符 – juharr