C# noob with a little problem here

  • C/++/#
  • Thread starter stonecoldgen
  • Start date
  • #1
stonecoldgen
107
0
Hi, so I have this code:


Code:
namespace Program1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Is the numer less than 10?");

      

            int numero = Console.Read();

            if (numero < 10)
            {
                Console.WriteLine("true");
            }
            else
            {
                Console.WriteLine("false");
            }

           

        }
    }
}




The console is always writing the line "false" even if I input a number that should return "true."


any idea what am I doing wrong?
 

Answers and Replies

  • #2
phinds
Science Advisor
Insights Author
Gold Member
2021 Award
17,805
9,864
maybe you have to cast the input into an integer as it comes in, else it converts to integer zero
 
  • #3
DavidSnider
Gold Member
502
144
Console.Read returns the ascii code for the key pressed, not the number itself.

Try this:
Code:
            int numero = Int32.Parse(Console.ReadLine());
 

Suggested for: C# noob with a little problem here

  • Last Post
Replies
10
Views
3K
  • Last Post
Replies
11
Views
3K
  • Last Post
Replies
7
Views
3K
  • Last Post
Replies
1
Views
2K
  • Last Post
Replies
3
Views
1K
  • Last Post
Replies
4
Views
3K
Replies
3
Views
2K
  • Last Post
Replies
19
Views
14K
  • Last Post
Replies
7
Views
2K
  • Last Post
Replies
2
Views
2K
Top