Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace lottery
- {
- class Program
- {
- static void Main(string[] args)
- {
- int[] guess = new int[5];
- int[] winningTicket = new int[5];
- Random r = new Random();
- //get ticket numbers from user
- for(int i = 0; i < guess.Length-1; i++)
- {
- guess[i] = getNum(1, 69);
- }
- // get last number
- guess[4] = getNum(1, 26);
- //generate winning ticket number
- for(int i = 0; i < winningTicket.Length-1; i++)
- {
- winningTicket[i] = r.Next(1, 69);
- }
- //generate last number
- winningTicket[4] = r.Next(1, 26);
- //print tickets
- Console.WriteLine("Your ticket");
- printTicket(guess);
- Console.WriteLine("Winning ticket");
- printTicket(winningTicket);
- }
- //prints out ticket numbers
- static void printTicket (int[] ticket)
- {
- foreach (int i in ticket)
- {
- Console.Write(i + " ");
- }
- Console.Write('\n');
- }
- //gets a number withen a range from user
- static int getNum(int bot, int top)
- {
- Console.Write($"Enter a ticket number between {bot} and {top}: ");
- //loop until input is a number and within range
- int num = isNum(Console.ReadLine());
- while(!(num>=bot) && !(num <= top))
- {
- Console.WriteLine("That is not a valid number");
- Console.Write($"Enter a ticket number between {bot} and {top}: ");
- num = isNum(Console.ReadLine());
- }
- //retrun valid input
- return num;
- }
- static int isNum(String num)
- {
- try
- {
- //try to parse num
- Int32.Parse(num);
- //string can be parsed to int, return parsed int
- return Int32.Parse(num);
- }catch(FormatException e)
- {
- //if format error occurs, string can not be converted to num, return -1
- return -1;
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment