4 Console Input Output 202ed6c75dafcbd047818c9ac26e7fd5
4 Console Input Output 202ed6c75dafcbd047818c9ac26e7fd5
Console.Write(…) Console.WriteLine(…)
Printing an integer variable Printing a string variable
int a = 15; string str = "Hello C#!";
... ...
Console.Write(a); // 15 Console.WriteLine(str);
Printing more than one variable using a Printing more than one variable using a
formatting string formatting string
double a = 15.5; string name = "Marry";
int b = 14; int year = 1987;
... ...
Console.Write("{0} + {1} = {2}", a, b, a + b); Console.WriteLine("{0} was born in {1}.", name, year);
// 15.5 + 14 = 29.5 // Marry was born in 1987.
Next print operation will start from the same line Next printing will start from the new line
5 6
1
3/14/2018
10
11
2
3/14/2018
13 14
Console.ReadKey() Console.ReadLine()
Waits until a combination of keys is pressed
Gets a line of characters
Reads a single character from console or a
Returns a string value
combination of keys
Returns null if the end of the input is reached
Returns a result of type ConsoleKeyInfo
KeyChar – holds the entered character Console.Write("Please enter your first name: ");
string firstName = Console.ReadLine();
Modifiers – holds the state of [Ctrl], [Alt], …
Console.Write("Please enter your last name: ");
string lastName = Console.ReadLine();
ConsoleKeyInfo key = Console.ReadKey();
Console.WriteLine(); Console.WriteLine("Hello, {0} {1}!",
Console.WriteLine("Character entered: " + key.KeyChar); firstName, lastName);
Console.WriteLine("Special keys: " + key.Modifiers);
15 16
3
3/14/2018
21
4
3/14/2018
25 26
3. A company has name, address, phone number, fax 5. Write a program that gets two numbers from the
number, web site and manager. The manager has console and prints the greater of them. Don’t use if
first name, last name, age and a phone number. statements.
Write a program that reads the information about a 6. Write a program that reads the coefficients a, b and c
company and its manager and prints them on the of a quadratic equation ax2+bx+c=0 and solves it
console. (prints its real roots).
27 28