Why doesn't Console.Read() return the number entered?

前端 未结 5 427
[愿得一人]
[愿得一人] 2020-12-11 14:32

this is my program that i wrote in C# in visual studio 2010 Ultimate and 2008 Team System:

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


        
相关标签:
5条回答
  • 2020-12-11 14:53

    As the documentation clearly states, Read() returns the index of the Unicode codepoint that you typed.

    0 讨论(0)
  • 2020-12-11 14:53

    I'm new to C#, but as far as I know, it's unnecessary to initialize your variable a when you create it. Another way to write your code could be:

    class Program
    {
        static void Main(string[] args)
        {
            int a;
            Console.WriteLine("Enter a number: ");
            a = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("you Entered : {0}", a);
            Console.ReadKey();
         }
    }
    
    0 讨论(0)
  • 2020-12-11 15:09

    Try this to reach your goal:

    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter a number: ");
            ConsoleKeyInfo a = Console.ReadKey();
            Console.WriteLine("you Entered : {0}",a.KeyChar);
            Console.ReadKey();
         }
    }
    
    0 讨论(0)
  • 2020-12-11 15:15

    Converted to a character code. Try:

    a = int.Parse(Console.ReadLine());
    
    0 讨论(0)
  • 2020-12-11 15:15

    The behavior you observed is described in the documentation.

    enter image description here

    0 讨论(0)
提交回复
热议问题