1 / 13

INPUT/OUTPUT STATEMENT ADDITION SLIDES

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

vielka-diaz
Download Presentation

INPUT/OUTPUT STATEMENT ADDITION SLIDES

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. INPUT/OUTPUT STATEMENT ADDITION SLIDES

  2. 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 • ….

  3. 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());

  4. 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.

  5. Console.Read() Read an ASCII Code of a character. ASCII Codeof a character is integer.

  6. 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.

  7. Example 1 static void Main() { int num; Console.Write(“Please input to num : ”); num = char.Parse(Console.Read()); Console.Write(num); } Compiler errors occurred.

  8. static void Main() { intnum; Console.Write("Please input to num : "); num = char.Parse(Console.ReadLine()); Console.Write(num); } Build finished successfully

  9. static void Main() { char alpha; Console.Write(“Please enter a character : ”); alpha = Convert.ToChar (Console.Read()); Console.Write(alpha); } Build finished successfully

  10. static void Main() { char alpha; Console.Write(“Please enter a character : ”); alpha = Convert.ToChar (Console.ReadLine()); Console.Write(alpha); } Build finished successfully

  11. 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’

  12. static void Main() { string alpha; Console.Write(“Please any name: ”); alpha = Convert.ToString(Console.Read()); Console.Write(alpha); } Build finished successfully

  13. static void Main() { string alpha; Console.Write(“Please any name: ”); alpha = Console.ReadLine(); Console.Write(alpha); } Build finished successfully

More Related