Csharp/CSharp Tutorial/Development/Console Read
Содержание
- 1 How to read a character entered using the keyboard
- 2 How to read a string entered using the keyboard
- 3 Input from the console using ReadLine().
- 4 Read a character from the keyboard.
- 5 Read a string from the keyboard, using Console.In directly
- 6 Reading Console Input
- 7 Read input from console
- 8 Read user selection from keyboard
How to read a character entered using the keyboard
class MainClass
{
public static void Main()
{
System.Console.Write("Enter a character: ");
char myChar = (char) System.Console.Read();
System.Console.WriteLine("You entered " + myChar);
}
}
Enter a character: q You entered
How to read a string entered using the keyboard
class MainClass
{
public static void Main()
{
System.Console.Write("Enter a string: ");
string myString = System.Console.ReadLine();
System.Console.WriteLine("You entered " + myString);
}
}
Enter a string: qwert You entered qwert
Input from the console using ReadLine().
using System;
class MainClass {
public static void Main() {
string str;
Console.WriteLine("Enter some characters.");
str = Console.ReadLine();
Console.WriteLine("You entered: " + str);
}
}
Enter some characters. qwer You entered: qwer
Read a character from the keyboard.
using System;
class MainClass {
public static void Main() {
char ch;
Console.Write("Press a key followed by ENTER: ");
ch = (char) Console.Read(); // get a char
Console.WriteLine("Your key is: " + ch);
}
}
Press a key followed by ENTER: Your key is:
Read a string from the keyboard, using Console.In directly
using System;
class MainClass {
public static void Main() {
string str;
Console.WriteLine("Enter some characters.");
str = Console.In.ReadLine(); // call TextReader"s ReadLine() method
Console.WriteLine("You entered: " + str);
}
}
Enter some characters. qwer You entered: qwer
Reading Console Input
- Console.In is an instance of TextReader.
- Console.In defines two input methods: Read() and ReadLine().
To read a single character, use the Read() method:
static int Read()
Read input from console
using System;
class MainClass {
public static void Main() {
string str;
Console.WriteLine("Enter password: ");
str = Console.ReadLine();
Console.WriteLine(str);
}
}
Enter password: asdf asdf
Read user selection from keyboard
using System;
class MainClass
{
public static int Main(string[] args)
{
Console.WriteLine("1 = A \n 2 = B \n3 = C\n");
Console.Write("Please select your implementation language:");
// Get choice.
string s = Console.ReadLine();
int n = int.Parse(s);
switch(n)
{
case 1:
Console.WriteLine("\nGood choice! C# is all about managed code.");
break;
case 2:
Console.WriteLine("\nLet me guess, your maintaining a legacy system?");
break;
case 3:
Console.WriteLine("\nVB.NET: It is not for just kids anymore...");
break;
default:
Console.WriteLine("\nWell...good luck with that!");
break;
}
return 0;
}
}
1 = A 2 = B 3 = C Please select your implementation language:1 Good choice! C# is all about managed code.