-3
public static void Main(string[] args)
    {
        Random numberGen = new Random();

        int num01 = numberGen.Next(1,1000001);
        int num02 = numberGen.Next(1,1000001);

        Console.WriteLine("What is " + num01 + " times " + num02 + " ?");

        int Answer = Convert.ToInt32(Console.ReadLine());
        if (Answer == num01 * num02)
        {
            Console.WriteLine("Well done your correct!");
        } 
        else
        {
            int responseIndex2 = numberGen.Next(1, 3);
            switch (responseIndex2) {

                case 1:
                    Console.WriteLine("You noob");
                    break;
                case 2:
                    Console.WriteLine("Are you trying uh?!");
                    break;
            }

        }

        Console.ReadKey();
    }

OK, so I can't find any replacement for Convert.ToInt32 and when I try to run the program its crashes when I answer I think it because of its too much numbers even tho its only 20 numbers?

Aviv
  • 129
  • 1
  • 11
  • 1
    You mean digits not numbers, huh? If that's what you mean, try `long Answer = Convert.ToInt64(Console.ReadLine());` instead – Ammar Jun 30 '19 at 12:13
  • `its crashes` Always be clear what the exception is, and on what line. – mjwills Jun 30 '19 at 12:30
  • Try following which sorts 100 unique numbers : Random rand = new Random(); int[] numbers = Enumerable.Range(0, 100).ToArray(); int[] randomNumbers = numbers.Select(x => new { number = x, rand = rand.Next() }).OrderBy(x => x.rand).Select(x => x.number).ToArray(); – jdweng Jun 30 '19 at 12:37

1 Answers1

1

A 32-bit signed integer (int) can store a maximum value of 2^31 - 1, which is less than the largest answer that your random math questions can have (the largest answer is 1000001 * 1000001, about 10^12).

Therefore, you should not use an int to store the answer. You could use a long instead. long is a 64-bit singed integer, whose maximum value is 2^63 - 1, which is about 9*10^18, far bigger than the largest answer your math questions can have.

You can use the corresponding Convert.ToInt64 to convert a string to a long:

long Answer = Convert.ToInt64(Console.ReadLine());
if (Answer == (long)num01 * (long)num02)
{
    Console.WriteLine("Well done your correct!");
} 
else
{
    int responseIndex2 = numberGen.Next(1, 3);
    switch (responseIndex2) {

        case 1:
            Console.WriteLine("You noob");
            break;
        case 2:
            Console.WriteLine("Are you trying uh?!");
            break;
    }

}
Sweeper
  • 213,210
  • 22
  • 193
  • 313