1

I want to read a double value and a integer value from a console application in C#. `

            int ch = Console.Read();
            Console.WriteLine("Enter a random integer");
            int x = int.Parse(Console.ReadLine());



           switch (ch)
            {
                case 1:

                    TempServiceRef.Service1Client s1 = new ConsoleTempApplication.TempServiceRef.Service1Client();
                    Console.WriteLine("Enter temperature");
                    string n = Console.ReadLine();
                    int param = int.Parse(n);
                    double result = s1.c2f(param);
                    Console.WriteLine(result);
                    Console.ReadLine();
                    break;

                case 2:

                    TempServiceRef.Service1Client s2 = new ConsoleTempApplication.TempServiceRef.Service1Client();
                    Console.WriteLine("Enter temperature");
                    int param1 = int.Parse(Console.ReadLine());
                    double result1 = s2.f2c(param1);
                    Console.WriteLine(result1);
                    Console.ReadLine();
                    break;

                default:
                    Environment.Exit(0);
                    break;
            }
            ` 

The console application closes once I try to enter my value for ch. Sometimes it closes after I give a value for temperature. I tried using tryParse and Convert.toInt. But I am not getting any results. Anyone who can help me with this? Further...I am just expecting the user to input only integer values(as an assumption). The tryparse usage in this case is not mandatory right?

4

1 回答 1

0

问题1:您正在使用该Console.Read()方法从控制台读取单个字符。但是在这里您需要按Enter key使用该方法时返回输入的值/字符。所以Console.Read()当您按EnterConsole.ReadLine()方法时将获得值空字符串并且 int.Parse() 在空字符串上抛出异常。

解决方案1:需要在读取第一个字符Console.ReadLine()的方法后面加上。Console.Read()然后你就可以继续了。

尝试这个:

            int ch =Console.Read();               
            Console.ReadLine(); //add this statement
            Console.WriteLine("Enter a random integer");
            int x = int.Parse(Console.ReadLine());

问题 2:您尝试将从控制台输入的字符直接与数字进行比较,这不起作用,因为当您从控制台读取字符时,它将存储其 ASCII 值而不是直接整数。例如,如果您1从控制台输入,它将被存储与49整数变量一样ch(ACII 代码 1 为 49),因此当您与 1 比较时,它不匹配。

解决方案2:所以你需要匹配它的字符值。因此将值括在单引号内以匹配在控制台中输入的确切值。

试试这个:用单引号将 1 和 2 括起来

            switch (ch)
            {
                case '1':  
                 ---
                 break;
                case '2':
                 -----
                 break;
            }

完整代码:

        int ch = Console.Read();
        Console.ReadLine();//Add this line to complete reading of a character
        Console.WriteLine("Enter a random integer");
        int x = int.Parse(Console.ReadLine());



       switch (ch)
        {
            case '1':

                TempServiceRef.Service1Client s1 = new ConsoleTempApplication.TempServiceRef.Service1Client();
                Console.WriteLine("Enter temperature");
                string n = Console.ReadLine();
                int param = int.Parse(n);
                double result = s1.c2f(param);
                Console.WriteLine(result);
                Console.ReadLine();
                break;

            case '2':

                TempServiceRef.Service1Client s2 = new ConsoleTempApplication.TempServiceRef.Service1Client();
                Console.WriteLine("Enter temperature");
                int param1 = int.Parse(Console.ReadLine());
                double result1 = s2.f2c(param1);
                Console.WriteLine(result1);
                Console.ReadLine();
                break;

            default:
                Environment.Exit(0);
                break;
        }
于 2014-01-26T04:54:05.987 回答