Question

I learn in school java but I need to use C# for a project that my friend needs. I converted the things from java that dont work in c# and changed them, but i got a problem that happens when I run the program. this is a part from it:

static void Main(string[] args)
{      
      Console.WriteLine("put N");
      int bign= Console.Read();
      Console.WriteLine("put n");
      int n = Console.Read();
      Console.WriteLine("put t");
      int t = Console.Read();
}

it only gets the N and then nothing happens. please help me :)

Was it helpful?

Solution

From Console.Read method

The Read method blocks its return while you type input characters; it terminates when you press the Enter key. Pressing Enter appends a platform-dependent line termination sequence to your input (for example, Windows appends a carriage return-linefeed sequence). Subsequent calls to the Read method retrieve your input one character at a time. After the final character is retrieved, Read blocks its return again and the cycle repeats.

As a solution, you can use Console.ReadLine() method and parse them to int with Int32.Parse or Int32.TryParse methods like;

int bign, n, t;
Console.WriteLine("put N");
string s1 = Console.ReadLine();
Console.WriteLine("put n");
string s2 = Console.ReadLine();
Console.WriteLine("put t");
string s3 = Console.ReadLine();
Int32.TryParse(s1, out bign);
Int32.TryParse(s2, out n);
Int32.TryParse(s3, out t);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top