简体   繁体   English

如何选择记录号最高的阵列号?

[英]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. 我正在输入4个房间的瓶子收集,当用户输入退出时,会显示房间收集的瓶子清单,并选择获胜者。 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. 您对rooms.Max()是关闭的,除了它将返回最大值并且您想要索引。

You could use a simple for loop to find the index. 您可以使用简单的for循环来查找索引。 Otherwise, you could go with an elegant "LINQ" method . 否则,你可以使用优雅的“LINQ”方法

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. 我想知道为什么LINQ没有包含IndexOf()扩展名。 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()));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 C# 中的字符串中提取一个数字 - How can I pick out a number from a string in C# 如何获得此Linq结果中的最高数字? - How can I get the highest number in this Linq result? 如何解析大型 DOCX 文件并挑选出在 python 中出现 n 次的关键字/字符串? - How can I parse a large DOCX file and pick out key words/strings that appear n number of times in python? 如何在给定数组中找到最高,第二高的数字,最低的第二低的数字 - How to find highest ,second highest number, Lowest Second Lowest number in given Array Android:如何记录应用程序的使用次数? - Android: How can I keep a record of the number of times an app was used? (C#)如何从随机数生成器中获取数字并在消息框中输出最高和最低数字? - (C#) How can I take numbers from my random number generator and output the highest and lowest number in a message box? 如何在 Visual C# 中获取数组中第二大的数字? - How to get the second highest number in an array in Visual C#? C#如何从List中获取包含x编号的数字? - C# How can I pick up numbers that contains x number from List? 如何为hyptherthreading / multicore选择最佳线程数? - How do I pick the best number of threads for hyptherthreading/multicore? 如何在不输入数字的情况下定义数学常数? - How can I define mathematical constants without typing out the number?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM