Presentation is loading. Please wait.

Presentation is loading. Please wait.

Using the Console.

Similar presentations


Presentation on theme: "Using the Console."— Presentation transcript:

1 Using the Console

2 Topics The Console Console Input Parsing String Data Console Output
Format Specifiers A Little Math

3 At the completion of this topic, students should be able to:
Objectives At the completion of this topic, students should be able to: Tell what the console is Describe how to read data from the console Parse String data into integers and doubles Describe how to write data to the console Explain how to use format specifiers Write C# programs that correctly read from the console write to the console format data for output

4 When a Console program executes, the C# runtime
environment automatically creates these two stream objects to help manage Console input and output. display buffer output buffer Console.Out program keyboard buffer keyboard buffer program Console.In

5 Console.ReadLine( ) The Console class provides the ReadLine( ) method to read data from the standard input stream, Console.In. This method waits for the user to type in some data and press the Enter key. The ReadLine( ) method returns the data that the user typed in as a string object. keyboard buffer keyboard buffer Console.In program

6 String name; name = ReadLine( ); John Doe keyboard buffer John Doe
Console.In name When using the Console class, be sure to add using static System.Console;

7 When dealing with numbers, we have to use the
Parse method to convert the string value into the appropriate numerical data type int age = 0; age = int.Parse(ReadLine( ) ); 25 keyboard buffer Parse method keyboard buffer 25 the string “25” age Console.In

8 When dealing with numbers, we have to use the
Parse method to convert the string value into the appropriate numerical data type double money = 0.0; money = double.Parse(ReadLine( ) ); 12.50 keyboard buffer Parse method keyboard buffer 12.50 The string “12.50” money Console.In

9 Console.WriteLine The Console class provides the WriteLine( ) method to write To the standard output stream, Console.Out. This method takes a string as it’s parameter. After writing to the display, the cursor is moved to the next line. display buffer output buffer Console.Out program

10 Console.WriteLine string name = “Joe Coder”; WriteLine( name );
display buffer Joe Coder output buffer Console.Out Joe Coder name

11 Console.WriteLine Numbers are automatically converted to
strings by the WriteLine( ) method: double money = 12.50 WriteLine( money); display buffer 12.50 output buffer Console.Out 12.50 money

12 Console.WriteLine You can combine string literals and numerical data
using the placeholder { .. } to mark the place where the numerical data should be displayed. double money = 12.50 WriteLine($ “You owe {money} to me.”); display buffer You owe to me output buffer Console.Out 12.50 money

13 Console.WriteLine You can use a format string to format the output
double money = 12.50 WriteLine($ “You owe {money:C} to me.”); display buffer You owe $12.50 to me output buffer Console.Out 12.50 money

14 Formatting Specifiers
D or d: Display an integer value as a decimal number F or f: Display a real value - default is two digits after the decimal point C or c: Display a real value as currency

15 With an integer you can use a number to
Formatting Strings With an integer you can use a number to indicate how many digits to display int number = 23; WriteLine($“The value is {number:D4}” ); The value is 0023

16 With a double you can use a number to indicate
Formatting Strings With a double you can use a number to indicate how many decimal digits to display double number = ; WriteLine($“The value is {number:F3}“); The value is

17 You can arrange things on the screen by using numbers
Formatting Strings You can arrange things on the screen by using numbers to specify a field width and justify the output in the field. double number = ; Console.WriteLine($“The value is {number, 6:F2}” ); 2 3 . 9 8 The value is _23.98 6 character field

18 Data displayed on a Graphical User Interface
must first be converted to a string. That string is then assigned to the Text Property of the control where you want to see it.

19 Suppose the name of this TextBox control is “outputTxtBox”
and we have an integer named sum to show there. outputTxtBox.Text = $”{sum:f2}”;

20 Practice Name one class that we have learned about in this lesson.

21 Practice Which method is used to convert data into its
character representation and send it to the standard output device? To what class does this method belong?

22 Practice Name a method that is used to convert numerical data
from its character representation and store it to in memory in its binary representation.

23 Practice Write a program that prints the message
“Hello, my name is Hal”. Then the program will prompt the user for his or her first name. It then Will print “Hello, user name, how are you?”

24 Practice Write a program that prints the message
“Hello, my name is Hal”. Then the program will ask the user for his or her name. Get the name and save it in a String object. Then print the message “Hello, user name, how are you?” Prompt the user to type in their age. Save it in an Integer. Then print the message “user name, you are n years old”

25 Practice Write a program that does the following:
Declares an integer, a double, and a character. In turn, asks the user to enter in an appropriate piece of data for each variable and stores it in that variable. Add together the integer and the double and the character. The result is stored in a double named sum. Print out the sum. Ask the user to type their name (first and last). Store their name in a string variable. Print out “Thank you (their name). Be able to discuss the results of your program.


Download ppt "Using the Console."

Similar presentations


Ads by Google