130 likes | 201 Views
INPUT/OUTPUT STATEMENT ADDITION SLIDES. Console.ReadLine(). string st = Console. ReadLine ();. Run-time error may be occurred if user’s input is incorrect. Console.ReadLine() Use to get the input ( String) from user Convert string to other data type
E N D
INPUT/OUTPUT STATEMENT ADDITION SLIDES
Console.ReadLine() stringst = Console.ReadLine(); Run-time error may be occurred if user’s input is incorrect • Console.ReadLine() • Use to get the input (String) from user • Convert string to other data type • int.Parse() Convert string to integer • double.Parse() Convert string to double • ….
static void Main() { double radius, area; string temp; Console.Write(“Please enter radius value : ”); temp = Console.ReadLine(); radius = double.Parse(temp); area = Math.PI * Math.Pow(radius, 2); Console.WriteLine(“Area of circle = {0:f2}”,area); } We may not have to declare string temp; Instead, combine 2 statements together as following: radius = double.Parse(Console.ReadLine());
public static void Main(string[] args) { intnum; Console.Write("Please input to num : "); num = int.Parse(Console.ReadLine()); Console.Write(num); Console.ReadKey(true); } Build finished successfully Run time error occurs if input is a character or string.
Console.Read() Read an ASCII Code of a character. ASCII Codeof a character is integer.
Console.Read() inti; Console.Write(“Input char:”); i = Console.Read(); Console.WriteLine(i); Input char: A 65 • Console.Read returns an ACSII value (Integer) of the code of the first typed character.
Example 1 static void Main() { int num; Console.Write(“Please input to num : ”); num = char.Parse(Console.Read()); Console.Write(num); } Compiler errors occurred.
static void Main() { intnum; Console.Write("Please input to num : "); num = char.Parse(Console.ReadLine()); Console.Write(num); } Build finished successfully
static void Main() { char alpha; Console.Write(“Please enter a character : ”); alpha = Convert.ToChar (Console.Read()); Console.Write(alpha); } Build finished successfully
static void Main() { char alpha; Console.Write(“Please enter a character : ”); alpha = Convert.ToChar (Console.ReadLine()); Console.Write(alpha); } Build finished successfully
static void Main() { char alpha; Console.Write(“Please enter a character : ”); alpha = Convert.ToString(Console.Read()); Console.Write(alpha); } Compiler errors occurred. Cannot implicitly convert type ‘string’ to ‘char’
static void Main() { string alpha; Console.Write(“Please any name: ”); alpha = Convert.ToString(Console.Read()); Console.Write(alpha); } Build finished successfully
static void Main() { string alpha; Console.Write(“Please any name: ”); alpha = Console.ReadLine(); Console.Write(alpha); } Build finished successfully