簡體   English   中英

如何選擇記錄號最高的陣列號?

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

我正在制作一個瓶子收集計划。 我正在輸入4個房間的瓶子收集,當用戶輸入退出時,會顯示房間收集的瓶子清單,並選擇獲勝者。 我的代碼現在選擇了最高記錄瓶數,但我希望它顯示具有該瓶數的房間號。 如何更改我的代碼以使其工作?

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() + "!!!");
        }
    }
}

嘗試這個:

        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 + "!!!");

您對rooms.Max()是關閉的,除了它將返回最大值並且您想要索引。

您可以使用簡單的for循環來查找索引。 否則,你可以使用優雅的“LINQ”方法

注意:不要忘記允許多個房間具有相同最大值的情況!

我想知道為什么LINQ沒有包含IndexOf()擴展名。 我最后編寫了自己的項目:

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;
    }
}

有了它你可以簡單地做:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM