用户输入类型是什么



我想写一个代码,从用户那里获得输入,如果用户给出了数字,则执行第一个操作,如果用户给定了"+"或"-",则执行第二个操作,并且如果用户给定诸如"q"或"e"之类的字符,则执行操作3,等等。有没有一种方法可以对所有数据类型(如bool或double(执行此操作?谢谢你的帮助!

do
{
Console.WriteLine("enter any number to do op 1");//this is an int
Console.WriteLine("press any letter to do op 2");//this is a char
Console.WriteLine("press '+' or '-' or '*' or '/' to do op 3"); // i mean any operator symbols (i dont know what type are these symbols!)
Console.WriteLine("enter any word to do op 4");//this is a string
Console.ReadLine();
//here i want something to realize what type is the user input?
if (//number -----> op 1)
if (// letter -----> op 2)
if (//+ or - or * or / -----> op3)
if (//word -----> op 4)
if (//user gave a combination of all types(a string that has numbers or symbols and etc) -----> tell him "enter a correct word only using letters")
} while (true); 

您似乎在寻找FiniteStateM机器:

while (true) {
Console.WriteLine("enter any number to do op 1");
Console.WriteLine("press any letter to do op 2");
Console.WriteLine("press '+' or '-' or '*' or '/' to do op 3"); 
Console.WriteLine("enter any word to do op 4");

string input = Console.ReadLine();
if (double.TryParse(input, out double value)) {
//input represents floating point value (op 1)
//TODO: put relevant code for op 1 here
}
else if (new string[] {"+", '-', "*", "/"}.Contains(input.Trim())) {
// input stands for opeartion +, -, *, / (op 3)
char op = input.Trim()[0];
//TODO: put relevant code for op 3 here 
}
else if (input.Length == 1) {
// input is one letter comand op (op 2)
char letter = input[0];
//TODO: put relevant code for op 2 here
}
else {
// input is some string (op 4)
string word = input;
//TODO: put relevant code for op 4 here
}
}

相关内容

  • 没有找到相关文章