/* (Start of program header annotation)
* Copyright and version Declaration of the program
* Copyright (c) 2011, a student from the computer College of Yantai University
* All rights reserved.
* File name: Create a Console application-enter a character and read the character using the Readkey () method of the Console class. Then, output the character on the Console.
* Author: Lei hengxin
* Completion date: January 1, September 07, 2012
* Version No.: V1.0
* Description of tasks and Solutions
* Input description:
* Problem description:
* Program output:
* End the comment in the program Header
*/
Method 1
[Csharp]
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Namespace ConsoleApplication_read_string
{
Class Program
{
Static void Main (string [] args)
{
Console. Write ("enter a character :");
Lelekeyinfo c = Console. ReadKey ();
Console. WriteLine ();
Console. WriteLine ("the character you entered is {0}", c. Key. ToString ());
Console. ReadKey (false );
}
}
}
Running result:
Another method:
[Csharp]
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Namespace ConsoleApplication_read_string
{
Class Program
{
Static void Main (string [] args)
{
Console. Write ("enter a character :");
String yesORno = Console. ReadKey (). Key. ToString (); // obtain the next character entered by the user and obtain the Console Key currently pressed.
Console. WriteLine ("the character you entered is {0}", yesORno );
ConsoleKey c = Console. ReadKey (). Key; // obtain the next character entered by the user and display it on the Console.
Console. WriteLine (); // line feed
Console. WriteLine ("the character you entered is {0}", c. ToString (); // output the information to the Console and wrap it
Console. ReadKey (false );
}
}
}
Running result:
Experience Accumulation:
1. method 1 the Code has forgotten to write comments. Pay attention later.
2. 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.