switch casedoes not work. It takes the data type of the arguments you pass in:
string x = Console.ReadLine();
switch(x) //x is the argument for switch
As it is. In your case, xthere is always string. The switch checks the value of the argument and finds the value configured casefor this value; it does not check the type of the argument and does not find it for it case.
- , string int, double, DateTime, , string, TryParse :
int myInt;
double myDouble;
bool r1 = int.TryParse(x, out myInt);
bool r2 = double.TryParse(x, out myDouble);
Edit:
switch, :
int a = (r1 ? 1 << 1 : 0) + (r2 ? 1 : 0); //0 if string, 1 if int, 2 if double, 3 if int or double
bit-flag, switch :
switch (a){
case 0: //string case
Console.WriteLine(x + "*");
break;
case 1: //int case
Console.WriteLine((Convert.ToInt32(x) + 1).ToString());
break;
case 2: //double case
Console.WriteLine((Convert.ToDouble(x) + 1).ToString());
break;
case 3: //int or double case
Console.WriteLine((Convert.ToInt32(x) + 1).ToString());
break;
}
:
- :
if (r1){
myInt += 1;
x = myInt.ToString();
} else if (r2){
myDouble += 1;
x = myDouble.ToString();
} else {
x = x + "*";
}
Console.WriteLine(x);