简体   繁体   中英

How can I pick out the array number with the highest record number?

I am making a bottle collection program. I am inputting bottles collects from 4 rooms and when the user types in quit, the list of bottles that rooms have collects is shown and the winner is picked. My code right now picked the highest recorded bottle count, but i want it to show the room number that has that bottle count. How can i change my code to make it work?

namespace BottleDrive
{
    class Program
    {
        static void Main(string[] args)
        {   //Initialize loop rooms to 4
            int[] rooms = new int[4];
            //Start of while loop to ask what room your adding into. 
            while (true)
            {
                Console.Write("Enter the room you're in: ");
                //If user enters quit at anytime, the code will jump out of while statement and enter for loop below
                string quit = Console.ReadLine();
                if (quit == "quit")
                    //Break statement separates embedded statement and is needed inorder to allow 
                    break; 


                //Variable room holds the number of bottles collect by each room. 
                int room = int.Parse(quit);
                Console.Write("Bottles collected in room {0}: ", room);
                // This line adds the count of bottles and records it so you can continuously count the bottles collected.
                rooms[room - 1] += int.Parse(Console.ReadLine());
            }
            //This for statement lists the 4 rooms and their bottle count when the user has entered quit.
            for (int i = 0; i < rooms.Length; ++i)
                Console.WriteLine("Bottles collected in room {0} = {1}", i + 1, rooms[i]);

            Console.WriteLine("And the Winner is room " + rooms.Max().ToString() + "!!!");
        }
    }
}

Try this:

        int maxValue = 0;
        int maxRoomNumber = 0;
        for (int i = 0; i < rooms.Length; ++i)
        {
            if (rooms[i] > maxValue)
            {
                maxValue = rooms[i];
                maxRoomNumber = i + 1;
            }
            Console.WriteLine("Bottles collected in room {0} = {1}", i + 1, rooms[i]);
        }

        Console.WriteLine("And the Winner is room " + maxRoomNumber + "!!!");

Your use of rooms.Max() is close except that it'll return the max value and you want the index.

You could use a simple for loop to find the index. Otherwise, you could go with an elegant "LINQ" method .

NOTE: Don't forget to allow for the case where multiple rooms have the same maximum value!

I've wondered myself why LINQ didn't include an IndexOf() extension. I ended up writing my own that I include in projects:

public static class Extensions
{
    public static int IndexOf<T>(this IEnumerable<T> items, Predicate<T> predicate)
    {
        int index = 0;
        foreach (T item in items)
        {
            if (predicate(item))
                break;
            index++;
        }

        return index;
    }

    public static int IndexOf<T>(this IEnumerable<T> items, T value)
    {
        int index = 0;
        foreach (T item in items)
        {
            if (item.Equals(value))
                break;
            index++;
        }

        return index;
    }
}

With that you could simply do:

Console.WriteLine("And the Winner is room " + rooms.IndexOf(rooms.Max()));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM