数组未返回最大值

时间:2018-03-08 05:31:11

标签: c# arrays

这是作业,但只是一小部分...... 我试图使用arr.MAX();返回数组中的最大数字,但我继续得到零。

调试后,我可以看到存储的值(来自用户)但它仍然返回零。

有问题的方法位于底部。

Class ElectionUI 
{
    public void candidateInfo() 
    {
         do
         {
             for (int i = 0; i < theElection.CandidateNames.Length; i++)
             {
                Console.Write("Please enter the name for Candidate #" + (i + 
                1) + ": ");
                theElection.CandidateNames[i] = Console.ReadLine();
                Console.Write("Please enter the number of votes for: {0}: ", 
                theElection.CandidateNames[i]);
                theElection.NumVotes[i] = int.Parse(Console.ReadLine());
                Console.WriteLine("");
             }
         } while (theElection.NumVotes.Length < 5);
    }
}
Class Election 
{
    private string[] candidateNames = new string[5];
    private int[] numVotes = new int[5];        
        //get/set Candidate Names
        public string[] CandidateNames 
        {
            get { return candidateNames; }
            set { candidateNames = value; }
        }

        //Get/Set Candidate votes
        public int[] NumVotes 
        {
            get { return numVotes; }
            set { numVotes = value; }
        }

    public void findWinner()
    {
        int max = NumVotes.Max();
            for (var i = 0; i < numVotes.Length; i++)
            {
                if (NumVotes[i] > max) 
                {
                    max = NumVotes[i];
                }
            }
        Console.WriteLine(max);
    }
}

3 个答案:

答案 0 :(得分:2)

从代码中不清楚,如何初始化选举类实例,以及如何调用findWinner方法。是的,你的Do-While循环无所事事。因为您已经将名称数组长度设置为5,所以它将运行for循环一次然后它将退出。即使你删除你的do-while,你也会获得相同的输出。

检查你的代码工作正常的小提琴。我假设您正在创建Election的实例,然后将其传递给ElectionUI类以使用它。

https://dotnetfiddle.net/oiVK9g

using System;
using System.Linq;

public class Program
{
    public static void Main()
    {
        var ele = new Election();
        var ui = new ElectionUI(ele);
        ui.candidateInfo();
        ele.findWinner();

    }
}

class ElectionUI 
{
    Election theElection;
    public ElectionUI(Election obj)
    {
        theElection = obj;
    }

    public void candidateInfo() 
    {
         do
         {
             for (int i = 0; i < theElection.CandidateNames.Length; i++)
             {
                Console.Write("Please enter the name for Candidate #" + (i + 
                1) + ": ");
                theElection.CandidateNames[i] = Console.ReadLine();
                Console.Write("Please enter the number of votes for: {0}: ", 
                theElection.CandidateNames[i]);
                theElection.NumVotes[i] = int.Parse(Console.ReadLine());
                Console.WriteLine("");
             }
         } while (theElection.NumVotes.Length < 5);
    }
}

class Election 
{
    private string[] candidateNames = new string[5];
    private int[] numVotes = new int[5];        
        //get/set Candidate Names
        public string[] CandidateNames 
        {
            get { return candidateNames; }
            set { candidateNames = value; }
        }

        //Get/Set Candidate votes
        public int[] NumVotes 
        {
            get { return numVotes; }
            set { numVotes = value; }
        }

    public void findWinner()
    {
        int max = NumVotes.Max();

        Console.WriteLine(max);
    }
}

答案 1 :(得分:0)

我想你想要回复谁赢了的候选人姓名,对吧?

使用您的代码,您应该将findWinner方法更改为:

public void findWinner()
{
    int max = NumVotes.Max();
    string winnerName = null;
    for (var i = 0; i < numVotes.Length; i++) {
        if (NumVotes[i] = max) {
            winnerName = CandidateNames[i];
        }
    }
    Console.WriteLine(winnerName);
} 

答案 2 :(得分:-1)

您需要使用max初始化本地变量Int32.MinValue。这样,遇到的任何值都将取代它。

相关问题