查找最高票数并匹配字符串数组

时间:2014-11-03 09:42:54

标签: c#

我正在试图弄清楚如何将候选人姓名与候选人投票相匹配,并显示最高投票和候选人姓名。

如何匹配我拥有的两个数组。

我知道我错过了什么,但是什么?我刚开始在家里学习C#。

namespace NumberOfVotes
{
    class Program
    {
        static void Main(string[] args)
        {
            int size, minVotes;
            int[] numOfCandidates;
            int[] numOfVotes;
            double avgMarks;

            string[] candidateName;

            Console.WriteLine("Enter number of candidates");
            size = int.Parse(Console.ReadLine());

            numOfCandidates = new int[size];
            candidateName = new string[size];
            numOfVotes = new int[size];

            for (int i = 0; i < numOfCandidates.Length; i++)
            {
                Console.WriteLine("Enter a Candidate Name");
                candidateName[i] = Console.ReadLine();

                Console.WriteLine("Enter number of votes thus far");
                numOfVotes[i] = int.Parse(Console.ReadLine());
            }

            int max = numOfVotes.Max();          
            avgMarks = numOfVotes.Average();
            minVotes = numOfVotes.Min();

            Console.WriteLine("Average votes: {0}", avgMarks);
            Console.WriteLine("Min number of votes is: {0}", minVotes);
        }
    }
}

5 个答案:

答案 0 :(得分:1)

您应该使用Dictionary

static void Main(string[] args)
{
    var candidates = new Dictionary<string, int>();
    Console.WriteLine("Enter number of candidates");
    var size = int.Parse(Console.ReadLine());

    for (int i = 0; i < size; i++)
    {
        Console.WriteLine("Enter a Candidate Name");
        var name = Console.ReadLine();

        Console.WriteLine("Enter number of votes thus far");
        var votes = int.Parse(Console.ReadLine());

        candidates.Add(name, votes);
    }

    Console.WriteLine("Average votes: {0}", candidates.Average(entry => entry.Value));
    Console.WriteLine("Min number of votes is: {0}", candidates.Min(entry => entry.Value));
}

答案 1 :(得分:1)

看到这些你可以用头脑思考的事情。如果您遇到问题,StackOverflow不会发布您的问题的网站,只有您遇到的问题需要一个可以帮助其他人的解决方案。

这样可行(对我来说最直接的方法):

int maxIndex = -1;
int max = -1;

for (int i = 0; i < numOfCandidates.Length; i++)
{
    Console.WriteLine("Enter a Candidate Name");
    candidateName[i] = Console.ReadLine();

    Console.WriteLine("Enter number of votes thus far");
    int num = int.Parse(Console.ReadLine()); // <-- unsafe

    // just check it every time, if the number is greater than the previous maximum, update it.
    if (num > max)
    {
       max = num;
       maxIndex = i;
    }

    numOfVotes[i] = num;
}

Console.WriteLine("Candidate {0}, with {1} votes, has the most votes", candidateName[maxIndex], max);

但是,如果你想要更多东西来计算(比如谁拥有最少的选票)而不做这些事情,你应该使用Dictionary<string, int>。这是一个与数字相关联的字符串,与投票相关的名称。

(有关此处的更多信息:http://www.dotnetperls.com/dictionary

答案 2 :(得分:0)

问题的解决方案就是做这样的事情:

int indexOfWinner = Array.IndexOf(numOfVotes, numOfVotes.Max());
string winner = candidateName[indexOfWinner];

我不知道你的C#和编程教育有多远(OOP等),所以你可能会发现有两点明显与否:

  • 您应该使用泛型集合而不是原始数组(在这种情况下为List<>)。
  • 你应该将所有这些封装成一个类。

我就是这样做的:

class Program
{
    static void Main(string[] args) {
        Candidates c = new Candidates("foo", "bar", "baz");
        Random rand = new Random();
        c.addVote(0, rand.Next(100));
        c.addVote(1, rand.Next(100));
        c.addVote(2, rand.Next(100));

        Console.WriteLine(c.getWinner());

        Console.WriteLine("number of votes:");
        Console.WriteLine(c[0] + ": " + c[0].numberOfVotes);
        Console.WriteLine(c[1] + ": " + c[1].numberOfVotes);
        Console.WriteLine(c[2] + ": " + c[2].numberOfVotes);
    }
}

class Candidates
{
    private List<Candidate> candidates;

    public Candidate this[string name] {
        get {
            return candidates.First(v => v.name == name);
        }
    }
    public Candidate this[int index] {
        get {
            return candidates[index];
        }
    }

    public Candidates(string firstCandidate, params string[] candidates) { //this is done to disable an empty constructor call
                                                                           //and also allow multiple candidates
        this.candidates = new List<Candidate>(candidates.Length);
        this.candidates.Add(new Candidate(firstCandidate));
        foreach(var c in candidates) {
            this.candidates.Add(new Candidate(c));
        }
    }

    public void addVote(int candidateNumber, int numberOfVotes = 1) {
        candidates[candidateNumber].numberOfVotes += numberOfVotes;
    }

    public Candidate getWinner() {
        candidates.Sort((candidate1, candidate2) => candidate2.numberOfVotes.CompareTo(candidate1.numberOfVotes));
        return candidates[0];
    }
}

class Candidate
{
    public string name { get; private set; }
    public int numberOfVotes { get; set; }

    public Candidate(string name) {
        this.name = name;
        this.numberOfVotes = 0;
    }

    public override string ToString() {
        return name;
    }
}

答案 3 :(得分:0)

您应该使用字典,但如果您想使用数组,请按照以下步骤操作:

avgMarks = numOfVotes.Average();

int avgIndex = numOfVotes.ToList().IndexOf(avgMarks);

Console.WriteLine("Average votes: {0} Candidate Names: {1}", avgMarks, candidateName[avgIndex]);

答案 4 :(得分:0)

您的代码运行正常。你要找的是具有最高票数的阵列的索引。此索引对于获得具有最高投票数的 candidateName 也很有用。因此,要获得该索引,只需使用从此行获得的最大值:

int max = numOfVotes.Max();

然后使用 IndexOf 静态方法查找数组中的max索引。为此尝试这行代码:

int index = Array.IndexOf<int>(numOfVotes, max);

现在只需打印出候选名称和最高票数,如下所示:

Console.WriteLine(candidateName[index] + " has highest number of vote : " + numOfVotes[index] );

您可以对来自DotNetPerlsMSDN

Array.IndexOf() 有一个清晰的概念
相关问题