First things first, I am an absolute beginner to c# so excuse me for any dumb mistakes I have done. currently, I am making a "banking system" as my beginner project. I am getting a frustrating error with entering a key.
var input = Convert.ToString(Console.ReadKey());
if (input == "E") {
Console.WriteLine("English has been selected");
}
if (input == "A")
{
Console.WriteLine("تم اختيار اللغة العربية");
}
else {
Console.WriteLine("Error!");
}
if I input E or A the console only displays the Else condition instead of the two if conditions. side note: vscode doesn't show any errors relating to this.
CodePudding user response:
Using ReadKey()
properly is important - its not quite as simple as say Python input()
but it does a good job once you understand it.
To go into a little detail, ReadKey()
returns a struct - an object called a ConsoleKeyInfo
- you can think of this like "information about the key pressed". What you are looking for is the key pressed represented as a character, so we want to get the Key property from the ConsoleKeyInfo
. But of course we aren't quite there that - the Key is still not a letter, but an Enum value. So finally it must be converted to a string... And although it is is a little complicated this provides a lot of benefit overall for writing safe and reliable programs - if I were to give a comparable example, it is like using a FileInfo
object to represent a file, rather than just a string name that is a filepath. But I do encourage you to read the docs referenced below because when you understand how to make use of documentation (and debugging tools) you have what you need to figure out most of this on your own!
To sum up, we get a ConsoleKeyInfo
struct, from which we get the Key property, and since that is an Enum we convert it to its "letter": Console.ReadKey().Key.ToString()
var input = Console.ReadKey();
if (input.Key.ToString() == "E")
{
Console.WriteLine("English has been selected");
}
else if (input.Key.ToString() == "A")
{
Console.WriteLine("تم اختيار اللغة العربية");
}
else
{
Console.WriteLine("Error!");
}
Reference: https://docs.microsoft.com/en-us/dotnet/api/system.console.readkey?view=net-6.0
Note that the ConsoleKeyInfo exposes another property called KeyChar which returns a character representing the key, so that is a different way to get the key pressed as a character that also works (and saves you the explicit ToString()
calls on the Key which we used above:
var input = Console.ReadKey();
if (input.KeyChar == 'E')
{
Console.WriteLine("English has been selected");
}
else if (input.KeyChar == 'A')
{
Console.WriteLine("تم اختيار اللغة العربية");
}
else
{
Console.WriteLine("Error!");
}
I do notice that in this case we must compare to characters ('A' not "A") - and this time a
and A
inputs are not treated the same, as they were before.
CodePudding user response:
I think, you are looking for somrthing like this:
// We want to save user choice (preferred language).
// Here I do it as simple as possible: 0 - English, 1 - Arabic
// A better choice not to use int but CultureInfo: CultureInfo language;
int language = 0;
// Infinite loop: we keep asking user until valid answer has been provided
while (true) {
// Let user know the rules: we expect either E or A
Console.WriteLine("Please, select language:");
Console.WriteLine(" E for Arabic");
Console.WriteLine(" E for English");
// If you want a single key press (no enter), let it be ReadKey
// Another possibility - key enter - Console.ReadLine()
var input = Console.ReadKey().KeyChar;
// If we have E - valid input - let user know and break the input loop
if (input == 'E') {
language = 0;
Console.WriteLine("English has been selected");
break;
}
// If we have A - valid input - let user know and break the input loop
if (input == 'A') {
language = 1;
Console.WriteLine("تم اختيار اللغة العربية");
break;
}
// All the other cases (any key except A and E) - let user know
// and keep on looking
Console.WriteLine("Error! Please, try again");
}
CodePudding user response:
Try this please:
using System;
class Program
{
public static void Main (string[] args)
{
string input = Console.ReadLine();
if (input == "E")
{
Console.WriteLine("English has been selected");
}
else if (input == "A")
{
Console.WriteLine("تم اختيار اللغة العربية");
}
else
{
Console.WriteLine("Error!");
}
}
}