Upload
vielka-diaz
View
18
Download
1
Embed Size (px)
DESCRIPTION
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 - PowerPoint PPT Presentation
Citation preview
INPUT/OUTPUT STATEMENTADDITION SLIDES
Console.ReadLine()
• 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
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){
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.
Read an ASCII Code of a character.
ASCII Code of a character is integer.
Console.Read()
Console.Read()
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: A65
6
static void Main(){ int num;
Console.Write(“Please input to num : ”); num = char.Parse(Console.Read()); Console.Write(num);}
Example 1
Compiler errors occurred.
static void Main(){ int num;
Console.Write("Please input to num : "); num = char.Parse(Console.ReadLine()); Console.Write(num);}
input Output1 492 50S 83
Sciences Run time error220 Run time error
Build finished successfully
static void Main(){ char alpha;
Console.Write(“Please enter a character : ”); alpha = Convert.ToChar (Console.Read()); Console.Write(alpha);}
input Output5 5
25 2S S
Sciences S
Build finished successfully
static void Main(){ char alpha;
Console.Write(“Please enter a character : ”); alpha = Convert.ToChar (Console.ReadLine()); Console.Write(alpha);}
input Output5 5
25 Run Time errorS S
Sciences Run Time error
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);}
input Output
1 49
100 49
2 50
25 50
S 83
Sciences S
Build finished successfully
static void Main(){ string alpha;
Console.Write(“Please any name : ”); alpha = Console.ReadLine(); Console.Write(alpha);}Build finished successfully
input Output
1 1
100 100
2 2
25 25
S S
Sciences Sciences