2017-04-09 47 views
0

我有這個代碼,我從中獲取用戶數據以創建車輛對象。唯一的問題是,因爲它們是不同的數據類型,我要求我必須在ReadReadLine之間切換。當代碼到達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); 
    } 
} 

任何幫助將不勝感激。

+0

取出'\ N'在'寫後使用此方法('也許吧。 –

+5

你應該只使用'ReadLine'和解析結果的類型你需要像'ID = int.Parse(Console.ReadLine());''Read''只是從控制檯讀取一個字符 – juharr

回答

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(); 
} 
+0

我非常欣賞,但是我怎麼用這個輸入來創建我的車輛對象我在上面的代碼中展示了嗎?對不起,我對c#非常陌生。 – kdean693

+0

請讓我看看'Vehicle'的定義。 –

+0

Added t他將類定義作爲下面的答案。 – kdean693

0

您應該使用Console.ReadLine();而不是Console.Read();,您可以通過difference-between-console-read-and-console-readline

發現他們的不同如果你仍然想使用Console.Read();請使用FlushKeyboard() method

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

,並呼籲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); 
} 
相關問題