Monday, October 21, 2013

Random and while loop for validation

a bit of a mess

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RandomNumbers
{
    class Program
    {
        int number;
        Random rand;
        static void Main(string[] args)
        {
            Program p = new Program();
            p.ValidateEntry();
            Console.ReadKey();
        }

        void GenerateRandomNumber()
        {
           rand = new Random();
           number = rand.Next(1, 1000);
            
            
        }

        void PopulateArray()
        {
        //    int[] grades = new int[5];
           GenerateRandomNumber();
        //    for (int i = 0; i < 5; i++)
        //    {
        //        rand.Next(0, 4);
        //        int x = rand.Next(10000, 100000);
        //        for (int j = 0; j < x; j++)
        //        {
        //        }
        //        grades[i] = number;
        //    }
        //    ReadArray(grades);
            number = rand.Next(1, 100);
            Console.WriteLine(number);
        }

        void ReadArray(int[] grades)
        {
            foreach (int i in grades)
            {
                Console.WriteLine(i);
            }
        }

        void ValidateEntry()
        {
            int[] myArray = new int[5];
            

            for (int i = 0; i < myArray.Length; i++)
            {
                int number = -1;
                while (number < 1 || number > 500)
                {
                    Console.WriteLine("Enter a number 1 and 500");
                    number = int.Parse(Console.ReadLine());
                    if (number < 1 || number > 500)
                    {
                        Console.WriteLine("invalid numbers");
                    }
                }
                myArray[i] = number;
               
            }

            ReadArray(myArray);
        }
    }
}

No comments:

Post a Comment