-1

I was just wondering why the console doesn't write my string, but instead shows "Press any key to close".

Would very much appreciate your help!

using System;

namespace oneToTen
{
    public class Conditionals
    {
        static void Main()
        {
        }
        public void NumberPicker()
        {
            Console.Write("Enter a number between 1-10");
            var input = Console.ReadLine();
            var number = Convert.ToInt32(input);
            if (number >= 1 && number <= 10)
            {
                Console.WriteLine("Valid");
            }
            else
            {
                Console.WriteLine("Invalid");
            }
        }
    }
}
Mostafiz
  • 7,243
  • 3
  • 28
  • 42
jwu
  • 13
  • 3
  • 1
    Because your main (the program's entry point) is empty :), you never invoke `NumberPicker()` anywhere. – sstan Oct 16 '16 at 04:43
  • Oh I see! However, I actually wanted my method to be named NumberPicker, therefore, should I nest it inside the main method? What do you suggest :( – jwu Oct 16 '16 at 05:06
  • You've got 2 valid options in the answers. Just keep in mind that the entry point will always be the `Main` method. – sstan Oct 16 '16 at 05:08
  • Mhmm, just saw them. Thank you, will definitely keep in mind Cheers bud. – jwu Oct 16 '16 at 05:11

2 Answers2

3

There is nothing in the Main() method.

I suppose you wanted to have it like this:

public static void Main()
{
    new Conditionals().NumberPicker();
}
Zbynek Vyskovsky - kvr000
  • 18,186
  • 3
  • 35
  • 43
1

Make NumberPicker method static and call it inside Main method

using System;

namespace oneToTen
{
   public class Conditionals
   {
      static void Main()
      {
          NumberPicker();
      }
      public static void NumberPicker()
      {
          Console.Write("Enter a number between 1-10");
          var input = Console.ReadLine();
          var number = Convert.ToInt32(input);
          if (number >= 1 && number <= 10)
          {
              Console.WriteLine("Valid");
          }
          else
          {
              Console.WriteLine("Invalid");
          }
       }
    }
}

and you can do everything inside main method in that case you don't need any extra method

static void Main()
 {
     Console.Write("Enter a number between 1-10");
     var input = Console.ReadLine();
     var number = Convert.ToInt32(input);
     if (number >= 1 && number <= 10)
     {
         Console.WriteLine("Valid");
     }
     else
     {
        Console.WriteLine("Invalid");
     }
 }
Mostafiz
  • 7,243
  • 3
  • 28
  • 42