0

my question is how to two numbers using Console.Read().When I run the code

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    {
        class Program
        {
            static void Main(string[] args)
            {
                int number1, number2, temp;

                Console.WriteLine("Enter the first number:\n");
                number1 = Console.Read();                       
                Console.WriteLine("\nEnter the Second number:\n");       
                number2 = Console.Read();
                Console.WriteLine("\nBefore swap, number1={0}, number2={1}\n", number1, number2);        
                temp = number1;
                number1 = number2;
                number2 = temp;
                Console.WriteLine("\nAfter swap, number1={0}, number2={1}\n", number1, number2);
            }
        }
    }

it only allows to enter one number, then come out the running results:

Enter the first number:
12
Enter the Second number:

Before swap, number1=49, number2=50;
After swap, number1=50,number2=49;

It doesn't execute the number1 that I entered. What are these 49,50 means?

I also write another code, it works fine:

         int number1, number2,temp;

       Console.WriteLine("Enter the first number:\n");
       number1 = Int32.Parse(Console.ReadLine());
       Console.WriteLine("\nEnter the Second number:\n");
       number2 = Int32.Parse(Console.ReadLine());
       Console.WriteLine("\nBefore swap, number1={0}, number2={1}\n", number1, number2);

       temp = number1;
       number1 = number2;
       number2 = temp;
       Console.WriteLine("\nAfter swap, number1={0}, number2={1}\n", number1, number2);
Simona
  • 29
  • 2
  • 6
  • The `Console` has no features for formatted input. Use `ReadLine()` and process the string. – H H Aug 20 '15 at 20:42
  • I think if you had waited between typing "1" and "2", you would have seen "Enter the second number" appear, which would have tipped you off that it wasn't taking "12" as a single input, but as two separate keypresses. – Adam V Aug 20 '15 at 20:45

2 Answers2

3

Console.Read() returns a character value, not the number entered. Plus, it only returns the first character typed in. A good way to achieve what you're trying is to write a custom method to read a number using Console.ReadLine() like so:

public static int ReadInt()
{
    string inputString = Console.ReadLine(); // Read the string
    int intValue;
    if (int.TryParse(inputString, out intValue)) // Try to parse the string, if it succeeds, it'll be put in intValue
    {
        return intValue;
    }
    return 0; // Invalid input, return 0 or something else
}
Philippe Paré
  • 4,279
  • 5
  • 36
  • 56
2

The issue is that you're not using Console.ReadLinein your first example; you're using Console.Read.

The first Console.Read takes in a single character - in your case, 1. According to this ASCII table, the 1 character corresponds to (int) 49. The second Console.Read similarly takes in the second character, 2, and converts it to 50.

Adam V
  • 6,256
  • 3
  • 40
  • 52