-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStringAndCharUserInput.cs
143 lines (116 loc) · 5.15 KB
/
StringAndCharUserInput.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
namespace CSharp.user_input_and_files;
public static class StringAndCharUserInput
{
//───────────────────────────────────────────────────────────────────────────────────
// ▬ (1) "ReadLine()" Method
// → is "Used" to "Read" a "Line"
// → of "Text Entered"
// → by the "User"
public static void ReadLineMethod()
{
// ▼ "Message" in "Console" ▼
Console.WriteLine("Enter a message: ");
// ▼ "Reading" & "Storing" the "User Input" in a "Variable" ▼
string? userInput = Console.ReadLine();
// ▼ "Printing" the "User Input" ▼
Console.WriteLine("You entered: " + userInput);
}
//───────────────────────────────────────────────────────────────────────────────────
// ▬ (2) "Read()" Method
// → is "Used" to Read a "Single Character"
// → from "User Input" ▬
public static void ReadMethod()
{
Console.WriteLine("\n\"Read()\" Method");
// ▼ "Message" in "Console" ▼
Console.WriteLine("Press a key:");
// ▼ "Reading" & "Storing" the "User Input" in a "Variable" ▼
int keyCode = Console.Read();
// ▼ "Printing" the "User Input" ▼
Console.WriteLine("The ASCII code of the entered character is: " + keyCode);
}
// public static void ReadMethod2()
// {
// Console.WriteLine("\nChange the Color of the Background");
//
//
// // ▼ "Message" in "Console" ▼
// Console.WriteLine("g = green, r = red, b = blue, w = white");
//
// // ▼ "Reading" & "Storing" the "User Input" in a "Variable" ▼
// int x = Console.Read();
//
// // ▼ Converting the "User Input" to a "Character" ▼
// char userInput = Convert.ToChar(x);
//
// // ▼ "While" Loop ▼
// while (userInput != 'z')
// {
// // ▼ "Switch" Statement ▼
// switch (userInput)
// {
// case 'g':
// Console.BackgroundColor = ConsoleColor.Green;
// break;
//
// case 'r':
// Console.BackgroundColor = ConsoleColor.Red;
// break;
//
// case 'b':
// Console.BackgroundColor = ConsoleColor.Blue;
// break;
//
// case 'w':
// Console.BackgroundColor = ConsoleColor.White;
// break;
//
// default:
// Console.WriteLine("Invalid Input");
// break;
// }
//
// // ▼ "Clear" the "Console" ▼
// Console.Clear();
//
// // ▼ "Message" in "Console" ▼
// Console.WriteLine("g = green, r = red, b = blue, w = white");
//
// // ▼ "ReSet" the "User Input" in a "Variable" ▼
// x = Console.Read();
//
//
// // ▼ ReConverting the "User Input" to a "Character" ▼
// userInput = Convert.ToChar(x);
// }
// }
//───────────────────────────────────────────────────────────────────────────────────
// ▬ (3) "ReadKey()" Method
// → is "Used" to "Read"
// → the "Key Pressed" by the "User"
// → "Without Requiring" the "Enter" Key
// → to be "Pressed" ▬
public static void ReadKeyMethod()
{
Console.WriteLine("\n\"ReadKey()\" Method: ");
// ▼ Declaring a "Console Key Info" ▼
ConsoleKeyInfo keyInfo;
// ▼ Enabling "Treat Control C As Input" ▼
Console.TreatControlCAsInput = true;
// ▼ "Loop" ▼
do
{
// ▼ "Setting" the "Console Key Info" ▼
keyInfo = Console.ReadKey();
// ▼ "Checking": If "Alt" is "Pressed" ▼
if ((keyInfo.Modifiers & ConsoleModifiers.Alt) != 0)
Console.Write(" key was pressed " + "(ALT + " + keyInfo.Key + ")");
// ▼ "Checking": If "Shift" is "Pressed" ▼
if ((keyInfo.Modifiers & ConsoleModifiers.Shift) != 0)
Console.Write(" key was pressed " + "(SHIFT + " + keyInfo.Key + ")");
// ▼ "Checking": If "Control" is "Pressed" ▼
if ((keyInfo.Modifiers & ConsoleModifiers.Control) != 0)
Console.Write(" key was pressed " + "(CTRL + " + keyInfo.Key + ")");
} while (keyInfo.Key != ConsoleKey.Escape);
}
}