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 unit; Console.Write("Please input to num : "); unit = int.Parse(Console.ReadLine()); Console.Write(unit); 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 7 public static void Main(string[] args) { int val1 = 6;float val2 = 4.2f; double val3 = 5.0000001;string val4 = "10.5"; char val5 = '7'; Console.WriteLine("1 : {0}",val1+val2); int con1 = (int)(val1+val2); Console.WriteLine("2 : {0}",con1); Console.WriteLine("3 : {0}",val2+val3); double con2 = val2+val3; Console.WriteLine("4 : {0}",con2); Console.WriteLine("5 : {0}",val3+val4); double con3 = val3 + double.Parse(val4); Console.WriteLine("6 : {0}",con3); Console.WriteLine("7 : {0}",val4+val5); int con4 = (int)double.Parse(val4) + (int)val5; Console.WriteLine("8 : {0}",con4);

8 8 Console.WriteLine("9 : {0}",val1+val5); int con5 = val1 + (int)val5 - '0'; Console.WriteLine("10 : {0}",con5); Console.ReadKey(true); } int val1 = 6; float val2 = 4.2f; double val3 = 5.0000001; string val4 = "10.5"; char val5 = '7';

9 public static void Main(string[] args) { int i0 = Console.Read(); char ch0 = (char)(Console.Read()); Console.WriteLine("i0 = {0}, ch0 = {1}",i0,ch0); int i1 = 65; char ch1 = (char)65; Console.WriteLine("i1 = {0}, ch1 = {1}",i1,ch1); int i2 = 'A'; char ch2 = 'A'; Console.WriteLine("i2 = {0}, ch2 = {1}",i2, ch2); Console.ReadKey(true); }

10 Examples of Read and ReadLine Statements

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

12 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

13 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

14 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

15 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’

16 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

17 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