Presentation is loading. Please wait.

Presentation is loading. Please wait.

Input/Output Statement Thanachat Thanomkulabut. Input Statement  Console.ReadLine()  Return string  Use to get the input from user  Convert string.

Similar presentations


Presentation on theme: "Input/Output Statement Thanachat Thanomkulabut. Input Statement  Console.ReadLine()  Return string  Use to get the input from user  Convert string."— Presentation transcript:

1 Input/Output Statement Thanachat Thanomkulabut

2 Input Statement  Console.ReadLine()  Return string  Use to get the input from user  Convert string to other data type  int.Parse() Convert string to integer  double.Parse() Convert string to double Example string st; st = Console.ReadLine(); Statement 2

3 Example: Input Statement Ex1: string myname; myname = Console.ReadLine(); Ex2: int Width; string temp1; temp1 = Console.ReadLine(); Width = int.Parse(temp1); Statement 3

4 Output Statements  Use the method Write or WriteLine in the Console class (which is in System namespace)  Basic usage:  Advanced usage:  Even more advanced usage: Console.WriteLine(”Size {0}x{1}”, width, height); double salary=12000; Console.WriteLine("My salary is {0:f2}.", salary); Console.WriteLine("Hello");Console.WriteLine(area); Statement 4

5 Test I 5  Write the program which  Input : Your name  Output : Your name is.

6 The Math Class Method/ Constant Value returnedExample CallResult PI Value of  Math.PI3.1415927 Max(x,y)Larger of the twoMath.Max(1,2)2 Abs(x)Absolute value of xMath.Abs(-1.3)1.3 Sqrt(x)Square-root of xMath.Sqrt(4.0)2.0 Round(x)Nearest integer to xMath.Round(0.8)1 Pow(x,y)xyxy Math.Pow(3,2)9.0 Log(x)Natural log of xMath.Log(10)2.302585 Ceiling(x)Smallest integer greater than or equal to x Math.Ceiling(4.1)5 Cos(x)Cosine of x radiansMath.Cos(Math.PI) Math Class 6

7 Test II 7  Write the program which  Input : Nothing  Output : cos( π /3). | ln(3) |

8 Outline 8  Data Types  Conversion  Read Statement  Write Statement

9 Data Types 9 TypeSizeDescriptionRange bool1 byteStore truth valuetrue / false char1 byteStore one charactercharacter code 0 – 255 byte1 byteStore positive integer0 – 255 short2 byteStore integer-32,768 -- 32,767 int4 byteStore integer-2.1 x 10 9 -- 2.1 x 10 9 long8 byteStore integer-9.2 x 10 18 -- 9.2 x 10 18 double16 byteStore real number± 5.0x10 -324 -- ± 1.7x10 308 stringN/AStore sequence of characters N/A

10 Outline 10  Data Types  Conversion  Read Statement  Write Statement

11 Convertion Number Convertion Type Convertion ConvertParse 11

12 Number Conversion  Number conversion can be done with command  = (type)  Example  byte b; int i; long l; double d;  i = (int)b;  l = (long)b+(long)i; 12 Bigger Smaller int byte long byte int long 100%

13  Example  byte b; int i; long l; double d;  d = (double)i /10;  b =(byte) l +3; Number Conversion  i =(int)d – 10; 13 int double byte long double int 100% May wrong in some case 100%

14 Type Conversion 14 Convertion Number Convertion Type Convertion ConvertParse

15  i = Convert.ToInt32(c);  c = Convert.ToChar(b);  st = Convert.ToString(i); Convert.To (); byte b=100; int i=100; double d=100.0; char c=‘d’; string st=“1”;  b = Convert.ToByte(i);  d = Convert.ToDouble(st); 15 int byte char int string double charbyte

16 .Parse();  Can Convert only from string to others  Example  d = double.Parse(st); 16 byte b=100; int i=100; double d=100.0; char c=‘d’; string st=“1”; string double  i = int.Parse(st);  c = char.Parse(st);  b = byte.Parse(st); stringint

17 Outline 17  Data Types  Conversion  Read Statement  Write Statement

18 Read Statement Read statement Console. Read() Console. ReadLine()  Console.Read();  Read Acsii code of first character  Console.ReadLine();  Read all character (in form of string)  Use with Convert or Parse commands frequently 18

19 Console.Read() Example 19 char c; int i; i = Console.Read(); Console.WriteLine(i); c = Convert.ToChar(i); Console.WriteLine(c); c char i int ABCD 65 A A A monitor

20 Console.ReadLine() Example 20 string st; st = Console.ReadLine(); Console.WriteLine(st); st string ABCD monitor Console.ReadLine() will always get input in string form

21 Console.ReadLine() Example  Console.ReadLine() will always get input in string type  if you would like other type you must convert it again  Example 21 string st; int i; st = Console.ReadLine(); i = int.Parse(st); string st; int i; i = int.Parse(Console.ReadLine()); OR

22 Outline 22  Data Types  Conversion  Read Statement  Write Statement

23 Write Statement Console. Write() Console. WriteLine()  Console.Write();  Show output  Console.WriteLine();  Show output and shift to new line 23

24 Write Statement Example 24 Console.Write(“Happy”); Console.Write(2009); Happy2009 Output : Console.WriteLine(“Happy”); Console.WriteLine(2009); Happy 2009 Output : string integer

25 Write Statement  Write() & WriteLine() can use in many form  Console.Write( );  Console.Write(“ ”,,...); 25 Hello 5 Console.Write(“Hello”); int a=5; Console.Write(a); string name = “Mc”; int score=9; Console.Write(“Score of {0} is {1}”,name,score); Score of Mc is 9 {0}{1}

26 Write Statement Example 26 int a=7, b=2; Console.WriteLine(“a={0} and b={1}”,a,b); Console.WriteLine(“{1}+{0}={2}”,b,a,a+b); a=7 and b=2 7 + 2 = 9 {0}{1} {0}{1}{2}

27 27 Write Statement : Example 27

28 Write Statement : Formatting Output 28 Console.WriteLine(“{0}”,10); 10 Console.WriteLine(“{0:F}”,10); 10.00 Console.WriteLine(“{0:X}”,10); A Console.WriteLine(“{0:D}”,10); 10 Console.WriteLine(“{0:P}”,10); 1,000.00 % Console.WriteLine(“{0:E}”,10); 1.000000E+001

29 Write Statement : Formatting Output 29 SymbolMeaningExampleOutput D, dDecimalConsole.WriteLine( “ {0:D} ",10};10 E, eExponentialConsole.WriteLine( “ {0:E} ",10};1.00000 0E+001 F, fFix PointConsole.WriteLine( “ {0:F} ",10};10.00 X, xHexadecimalConsole.WriteLine( “ {0:X} ",10};A P, pPercentConsole.WriteLine( “ {0:P} ",10};1,000.0 0 %

30 Special Character Symbols Output \’Single Quote \”Double Quote \\Backslash \nNew line \tHorizontal tab  What should you do if you would like to WriteLine “C:\mydocument” 30 Console.WriteLine(““C:\mydocument””); Console.WriteLine(“\“C:\mydocument\””); Console.WriteLine(“\“C:\\mydocument\””);

31 Test III 31  Write the program which  Input : 3 number  Output : average of 3 input number

32 Test IV 32  Write the program which  Input : lenght of radius of circle  Output : Area of circle

33 Any question?


Download ppt "Input/Output Statement Thanachat Thanomkulabut. Input Statement  Console.ReadLine()  Return string  Use to get the input from user  Convert string."

Similar presentations


Ads by Google