C # Console class

Source: Internet
Author: User

 

Console. Write: directly writes strings to the Console without line breaks. You can continue writing the previous characters.
Console. WriteLine indicates to write a string to the Console and then wrap it.
Console. Read indicates reading strings from the Console without line breaks.
Console. ReadLine indicates reading strings from the Console and wrapping them.
Console. ReadKey gets the next character or function key that the user presses. The pressed key is displayed in the Console window.
Console. Beep play the prompt sound through the Console speaker.
Console. Clear clears the Console Buffer and the display information of the corresponding Console window.
 

Output to console
Output to the console is to output the data to the console and display it .. Net Framework provides the console class to implement this task

The output method is as follows:
Console. WriteLine ();
Console. Write ();
Console. WriteLine (output value );
Console. Write (output value );
Console. WriteLine ("output format string", variable list );
Console. Write ("output format string", variable list );

The only difference between Console. WrietLine () and Console. Write () is that the former outputs a new line while the latter does not.
Console. WriteLine ("{0}'s wives in the deer Ding note include {1}, {2}, {3}, and other 7", strName [0], strName [1], strName

[2], strName3]);
This method contains two parameters: "Format String" and Variable list. "{0}'s wives are {1}, {2}, {3}, and so on." This is

Format String. {0}, {1}, {2}, and {3} are placeholders. They represent the variable tables listed in sequence. 0 corresponds to the first of the Variable list.

Variables. 1 corresponds to the 2nd variables in the Variable list, and so on.

 

Enter
The Console class provides the following input methods:
Console. ReadLine ();
This code returns a string type data, which can be directly assigned to a string variable, such:
String strname = Console. ReadLine ();
Sometimes you need to enter a number in the console to use the preceding content for data conversion, such:
Int num = int. Pares (Console. ReadLine ());
Int num = Convert. ToInt32 (Console. ReadLine ());
The above two statements have the same effect. You can select either of them based on your habits.

Note: The input results of Console. ReadLine () and Console. Read () are completely different and cannot be mixed.
Console. Read (), return the ASCII code of the first character
Console. ReadLine (), returns a string
That is to say, the read method can only read the first character, while ReadLine can read multiple characters and can also read line breaks.

 

The function of Console. ReadKey (). read reads data from the Console. The key indicates that the keyboard is pressed.

Obtain that the user presses the function key and displays it in the window. It is used in the previous code to suspend the window. In the debugging status, only press

When any key is set, the window is closed.

 

 

Console Input and Output

Using System;
Using System. Collection. Generic;
Using System. Linq;
Using System. Text;
Namespace ConsoleTest
{
Class ConsoleTest
{
Static void Main (string [] args)
{
Console. WriteLine ("enter the names of two students ");
String name1 = Console. ReadLine ();
String name2 = Console. ReadLine ();
Console. WriteLine ("Enter the scores of two students ");
Int score1 = int. Parse (Console. ReadLine ());
Int score2 = int. Parse (Console. ReadLine ());
Console. WriteLine ("first student's name {0}, score {1}", name1, score1 );
Console. WriteLine ("second student's name {0}, score {1}", name2, score2 );
Console. ReadKey ();
}
}
}

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.