Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- class Program
- {
- public static void PrintHelp()
- {
- Console.WriteLine("Coding Conundrums Problem 1.2 Solution: Number Shuffle");
- Console.WriteLine("------------------------------------------------------");
- Console.WriteLine("Problem Description:");
- Console.WriteLine(" I want a program that reads in 20 integers and tells me how much of each number");
- Console.WriteLine(" was entered. I actually have a use for this, in that each day I sell 20 pairs of");
- Console.WriteLine(" shoes and I want to find out the most popular shoe sizes. It's amazing that I");
- Console.WriteLine(" manage to hold down my lecturing job, what with all the shoe sales I'm involved");
- Console.WriteLine(" with.");
- Console.WriteLine();
- Console.WriteLine("Use it like this: ");
- Console.WriteLine(" shoesales.exe");
- }
- ///<summary>
- ///Gets an integer from the user between min and max inclusive.
- ///</summary>
- ///<param name="prompt">The prompt to display to the user.</param>
- ///<param name="min">The minimum value the user should enter.</param>
- ///<param name="max">The maximum value the user should enter.</param>
- static int ReadNumber(string prompt, int min, int max)
- {
- int number;
- while (true)
- {
- Console.Write(prompt);
- try
- {
- number = int.Parse(Console.ReadLine().Trim());
- }
- catch
- {
- Console.WriteLine("That was not a number. Numbers may contain numeric characters only.");
- continue;
- }
- if (number < min)
- {
- Console.WriteLine("That number was too low. Please enter a number between " + min + " and " + max + ".");
- continue;
- }
- if (number > max)
- {
- Console.WriteLine("That number was too high. Please enter a number between " + min + " and " + max + ".");
- continue;
- }
- break;
- }
- return number;
- }
- public static void PrintShoeSizes(Dictionary<int, int> shoes)
- {
- Console.WriteLine("Shoe Sizes:");
- Console.WriteLine("-----------");
- Console.WriteLine("Size Count");
- foreach(KeyValuePair<int, int> shoeSize in shoes)
- {
- Console.WriteLine("{0,-8} {1}", shoeSize.Key, shoeSize.Value);
- }
- }
- public static void Main(string[] args)
- {
- if(args.Length == 1 && args[0].ToLower().Trim('-') == "help")
- {
- PrintHelp();
- return;
- }
- int shoeTotal = 20;
- Dictionary<int, int> shoes = new Dictionary<int, int>();
- for(int i = 0; i < shoeTotal; i++)
- {
- PrintShoeSizes(shoes);
- int currentShoeSize = ReadNumber(String.Format("Enter the shoe size #{0}: ", i + 1), 1, 12);
- if(!shoes.ContainsKey(currentShoeSize))
- {
- shoes.Add(currentShoeSize, 1);
- }
- else
- {
- shoes[currentShoeSize] += 1;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement