C # -- 2nd week experiment -- Task 2 (1) -- enter a character and read the character using the Readkey () method of the Console class.

Source: Internet
Author: User

/* (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.

Related Article

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.