如何正确添加ConsoleKeyInfo以立即返回值

时间:2018-02-04 03:46:36

标签: visual-studio-2017 console-application

我明白了。我用Google搜索了这个问题,发现有这个问题可以使用:

option = Convert.ToChar(Console.ReadKey(true).KeyChar);

我已经谷歌搜索和故障排除了好几天。我想让这个计算器程序在操作员选择一个函数(+-*/)后立即返回一个值。从阅读和其他示例看,这ConsoleKeyInfo应该有效:         ConsoleKeyInfo info = Console.ReadKey();         string operation = info.Key.ToString();     当我添加它时,我无法使程序正常运行。我明白了:

    system.formatexception string must exactly be one character long 
My current code works, but once you select a function (+), you then have to press enter to get the answer.
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace ConsoleApp5
    {
        class Program
        {
            static void Main(string[] args)
            {

                int Num1, Num2, result;
                char option;
                Console.Write("Enter the First Number : ");
                Num1 = Convert.ToInt32(Console.ReadLine());
                Console.Write("Enter the Second Number : ");
                Num2 = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("Select: +, -, *, /");


                option = Convert.ToChar(Console.ReadLine());
                switch (option)
                {
                    case '+':
                        result = Num1 + Num2;
                        Console.WriteLine("The result of Addition is : {0}", result);
                        break;
                    case '-':
                        result = Num1 - Num2;
                        Console.WriteLine("The result of Subtraction is : {0}", result);
                        break;
                    case '*':
                        result = Num1 * Num2;
                        Console.WriteLine("The result of Multiplication is : {0}", result);
                        break;
                    case '/':
                        result = Num1 / Num2;
                        Console.WriteLine("The result of Division is : {0}", result);
                        break;
                    default:
                        Console.WriteLine("Invalid Option");
                        break;
                }
                Console.ReadLine();
            }
        }
    }

**

0 个答案:

没有答案