Presentation is loading. Please wait.

Presentation is loading. Please wait.

INPUT/OUTPUT STATEMENT ADDITION SLIDES. Console.ReadLine() – Use to get the input (String) from user Convert string to other data type – int.Parse() 

Similar presentations


Presentation on theme: "INPUT/OUTPUT STATEMENT ADDITION SLIDES. Console.ReadLine() – Use to get the input (String) from user Convert string to other data type – int.Parse() "— Presentation transcript:

1 INPUT/OUTPUT STATEMENT ADDITION SLIDES

2 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 – …. string st = Console.ReadLine(); Run-time error may be occurred if user’s input is incorrect 2

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) { int num; 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 Read an ASCII Code of a character. ASCII Code of a character is integer. Console.Read()

6  Console.Read returns an ACSII value (Integer) of the code of the first typed character. int i; Console.Write(“Input char:”); i = Console.Read(); Console.WriteLine(i); Input char: A 65 6

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

8 static void Main() { int num; Console.Write("Please input to num : "); num = char.Parse(Console.ReadLine()); Console.Write(num); } inputOutput 149 250 S83 SciencesRun time error 220Run time error Build finished successfully

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

10 static void Main() { char alpha; Console.Write(“Please enter a character : ”); alpha = Convert.ToChar (Console.ReadLine()); Console.Write(alpha); } inputOutput 55 25Run Time error SS SciencesRun Time error 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); } inputOutput 149 10049 250 2550 S83 SciencesS Build finished successfully

13 static void Main() { string alpha; Console.Write(“Please any name : ”); alpha = Console.ReadLine(); Console.Write(alpha); } Build finished successfully inputOutput 11 100 22 25 SS Sciences


Download ppt "INPUT/OUTPUT STATEMENT ADDITION SLIDES. Console.ReadLine() – Use to get the input (String) from user Convert string to other data type – int.Parse() "

Similar presentations


Ads by Google