调用包含数组的函数

时间:2014-06-08 01:19:04

标签: c# arrays function reference

我现在正忙于完成这项家庭作业。我遇到的问题是我似乎无法将函数/方法调用到Main方法,因此可以执行它。由于某种原因,阵列不会通过。我必须通过引用传递数组(我似乎没有任何问题)和平均得分值。任何帮助将不胜感激。

编辑:所以我似乎已经修好了... kinda。但我遇到了最后一个功能的问题; DisplayBelowAverage。

我收到2个错误:Argument1:无法从' refString []转换为' ref string'和最佳重载方法匹配Lab5_exA.Program.DisplayBelowAverage(ref string,ref double [],ref double,ref int)'有一些无效的论点。

我似乎无法找到问题发生的位置。它指的是我的数组:playerName,当我选择错误但我无法在任何地方看到问题,可能是" ref string"在第一个错误中没有数组符号" []"当其他数组显示时。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lab5_exA
{
class Program
{

    public static double InputData(ref string[] playerName, ref double[] playerScore, ref int players, ref double totalScore)
    {
        do
        {
            Console.Write("Enter the player name or type 'Q' or 'q' to quit.."); //get player name
            playerName[players] = Console.ReadLine();

            if (playerName[players] == "Q" || playerName[players] == "q") //checks for exit value
            {
                break;
            }

            Console.WriteLine("Enter the player's score.."); //get player score
            playerScore[players] = Convert.ToDouble(Console.ReadLine());

            totalScore += playerScore[players];

            players++; //establish size of array

        } while (true);
        return 0;
    }

    public static double DisplayPlayerData(ref string[] playerName, ref double[] playerScore, ref int players)
    {
        for (int w = 0; w < players; w++)
        {
            Console.WriteLine("Player: " + playerName[w] + "\t" + "Score: " + playerScore[w]);
        }

        return 0;
    }

    public static double CalculateAverageScore(ref int players, ref double totalScore, ref double averageScore)
    {
        averageScore = totalScore / players;
        return averageScore;
    }

    public static double DisplayBelowAverage(ref string playerName, ref double[] playerScore, ref double averageScore, ref int players)
    {
        int z = 0;

        Console.WriteLine("Average Score: " + averageScore);
        Console.WriteLine("The following players received a score below the average score..");
        Console.WriteLine("Name \t Score");
        Console.WriteLine("---- \t ----");

        while (z < players)
        {
            int y = 0;

            if (playerScore[y] < averageScore)
            {
                Console.WriteLine(playerName[y] + "\t" + playerScore[y]);
            }
        }
        return 0;
    }

    static void Main(string[] args)
    {
        string[] playerName = new string [100];
        double[] playerScore = new double [100];
        int players = 0;
        double averageScore = 0;
        double totalScore = 0;

        InputData(ref playerName, ref playerScore, ref players, ref totalScore);
        DisplayPlayerData(ref playerName, ref playerScore, ref players);
        CalculateAverageScore(ref players, ref totalScore, ref averageScore);
        DisplayBelowAverage(ref playerName, ref playerScore, ref averageScore, ref players);

        Console.Read();

    }
}

}

1 个答案:

答案 0 :(得分:1)

public static double DisplayBelowAverage(ref string playerName, ref double[] playerScore, ref double averageScore, ref int players){

替换为

public static double DisplayBelowAverage(ref string[] playerName, ref double[] playerScore, ref double averageScore, ref int players){

您正在使用它和数组并传递数组,但方法声明需要方括号。