Retrieving data from Console.ReadLine input

I am new to programming and I have a problem, but I need your help. My task is to write a program that reads something from the console, and then if its number is printed 1, if its line looks like this (line + *). Heres my code, but there is something wrong, and I cannot figure it out. He must use a switch-case.

static void Main(string[] args)
{
    string x = Console.ReadLine();
    switch (x)
    {
        case "int" :
            int i = int.Parse(x);
            i = i + 1;
            Console.WriteLine(i);
            break;
        case "double":
            double d = double.Parse(x);
            d = d + 1;
            Console.WriteLine(d);
            break;
        case "string":
            string s = (x);
            Console.WriteLine(s + "*");
            break;
        default:
            break;
    }        
}  
+4
source share
2 answers

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); //r1 is true if x can be parsed to int
bool r2 = double.TryParse(x, out myDouble); //r2 is true if x can be parsed to double

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){ //parsable to int
  //do something, like raise the number by 1
  myInt += 1;
  x = myInt.ToString();      
} else if (r2){ //parsable to double
  //do something, like raise the number by 1
  myDouble += 1;
  x = myDouble.ToString();
} else { //cannot be parsed to any
  //do something like add `*`
  x = x + "*";
}
Console.WriteLine(x);
+2

, , int, double, string,

 public static class Extenstions
{
    public static bool IsValid<T>(this string source) where T : struct
    {
        MethodInfo tryParse = (MethodInfo)typeof(T).GetMember("TryParse").FirstOrDefault();
        if (tryParse == null) return false;
        return (bool)tryParse.Invoke(null, new object[] { source, null });
    }
    public static Type GetParsableType(this string source)
    {
        return source.IsValid<int>()&&!source.Contains(".") ? typeof(int) : source.IsValid<double>() ? typeof(double) : typeof(string);

    }
}
class Program
{

    static void Main(string[] args)
    {
        while (true)
        {
            string x = Console.ReadLine();
            switch (x.GetParsableType().Name.ToLower())
            {
                case "int32":
                case "int":
                    int i = int.Parse(x);
                    i = i + 1;
                    Console.WriteLine(i); break;
                case "double":
                    double d = double.Parse(x);
                    d = d + 1;
                    Console.WriteLine(d); break;
                case "string":
                    string s = (x);
                    Console.WriteLine(s + "*"); break;
                default: ; break;
            }
        }
    }
}
+1

All Articles