예제 #1
0
        static void Main(string[] args)
        {
            var    teams = new List <SoccerTeam>();
            string input; // To use for reading lines in. The variable should be immediately transferred away from input to avoid being overridden.

            // Get the number of soccer teams
            int numTeams = getValidInt("How many teams? ");

            // Get each team's information
            for (int i = 0; i < numTeams; i++)
            {
                string name;
                int    points;

                // Get the team's name
                Console.Write("\n\nEnter Team " + (i + 1) + "'s name: ");
                input = Console.ReadLine();
                name  = UppercaseFirst(input);

                // Get the team's points
                points = getValidInt("\nEnter " + name + "'s points: ");

                // Add the team to the teams list
                var team = new SoccerTeam(name, points);
                teams.Add(team);
            }

            // Sort the teams by points
            teams = teams.OrderByDescending(t => t.points).ToList();

            Console.WriteLine("\n\nHere is the sorted list:\n");

            // Print Column Headings
            Console.Write("\nPosition".PadRight(25, ' ') + "Name".PadRight(25, ' ') + "Points".PadRight(25, ' '));
            Console.Write("\n--------".PadRight(25, ' ') + "----".PadRight(25, ' ') + "------".PadRight(25, ' '));

            int position = 1;

            foreach (var t in teams)
            {
                t.print(position);
                position++;
            }

            Console.ReadLine();
        }
예제 #2
0
        static void Main(string[] args)
        {
            bool bPlayAgain = true;

            while (bPlayAgain)
            {
                // Declare Variables
                int iNumTeams;
                int iCount = 0;

                // Create a new list of SoccerTeam objects
                List <SoccerTeam> stTeams = new List <SoccerTeam>();

                // Prompt the user to enter the number of teams to be included in the
                // tournament simulation.
                iNumTeams = RequestIntFromUser("How many teams?");

                // Ask the user to enter the team's name and points to create a new
                // soccer team object. Program exits the loop when the number of teams has
                // reached the number indicated eariler by the user (iNumTeams)
                while (iCount < iNumTeams)
                {
                    // Variables
                    string sTeamName;
                    int    iTeamPoints;

                    // Prompt the user to input the team name
                    Console.Write("\n\nEnter Team " + (iCount + 1) + "'s Name: ");
                    sTeamName = UppercaseFirst(Console.ReadLine());

                    Console.WriteLine();

                    // Prompt the user to input the team's points
                    iTeamPoints = RequestIntFromUser("Enter " + sTeamName + "'s points:");

                    // Create new SoccerTeam object and add it to the List created previously
                    SoccerTeam stNewTeam = new SoccerTeam(sTeamName, iTeamPoints);
                    stTeams.Add(stNewTeam);
                    iCount++;
                }


                // Sort the teams by their points in descending order
                List <SoccerTeam> sortedTeams = stTeams.OrderByDescending(SoccerTeam => SoccerTeam.points).ToList();

                // Write tournament results table header to the console
                Console.WriteLine("\n\nHere is the sorted list:");
                Console.WriteLine("\nPosition           Name                       Points");
                Console.Write("--------           ----                       ------");

                // iCount is reset to 1 so that it can be used as the Position number
                // in the next foreach loop
                iCount = 1;

                // Write tournament results to the console
                foreach (SoccerTeam st in sortedTeams)
                {
                    Console.Write("\n" + Convert.ToString(iCount).PadRight(19, ' '));
                    Console.Write(Convert.ToString(st.name).PadRight(27, ' '));
                    Console.Write(Convert.ToString(st.points));
                    iCount++;
                }

                // Program waits 1 second before asking the user if they want to play again
                Console.CursorVisible = false;
                Thread.Sleep(1000);
                Console.CursorVisible = true;

                Console.Write("\n\n\nWould you like to play again? (Y/N) ");
                string s = Console.ReadLine().ToUpper();

                // Evaluate response from user
                if (s.Contains("Y"))
                {
                    // Remove play again question and add a line to seperate the 1st game from the second
                    Console.CursorTop--;
                    Console.Write("                                                                         ");
                    Console.CursorLeft = 0;
                    Console.WriteLine("-------------------------------------------------------------------\n\n");
                }
                else
                {
                    bPlayAgain = false;
                }
            }

            Console.WriteLine("\n\n\nThanks for playing!");
            Thread.Sleep(1000);
        }