Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 调用包含数组的函数_C#_Arrays_Function_Reference - Fatal编程技术网

C# 调用包含数组的函数

C# 调用包含数组的函数,c#,arrays,function,reference,C#,Arrays,Function,Reference,我现在在做这个家庭作业时感到既茫然又疲惫。我遇到的问题是,我似乎无法将函数/方法调用到Main方法,以便执行它。由于某种原因,数组无法通过。我必须通过引用传递数组(我似乎没有任何问题),并通过值传递平均分数。如蒙协助,将不胜感激 编辑:看来我已经修好了……有点。但我对最后一个函数有问题;显示你的愤怒 我收到2个错误:Argument1:无法从'refString[]转换为'ref string',Lab5_exA.Program的最佳重载方法匹配。DisplayBelowAverage(ref

我现在在做这个家庭作业时感到既茫然又疲惫。我遇到的问题是,我似乎无法将函数/方法调用到Main方法,以便执行它。由于某种原因,数组无法通过。我必须通过引用传递数组(我似乎没有任何问题),并通过值传递平均分数。如蒙协助,将不胜感激

编辑:看来我已经修好了……有点。但我对最后一个函数有问题;显示你的愤怒

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

我似乎找不到问题发生的地方。当我选择错误时,它指的是我的array: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();

    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
名称空间Lab5_exA
{
班级计划
{
公共静态双输入数据(参考字符串[]玩家名称,参考双[]玩家核心,参考整数玩家,参考双总分数)
{
做
{
Console.Write(“输入玩家名称或键入'Q'或'Q'退出..”);//获取玩家名称
playerName[players]=Console.ReadLine();
if(playerName[players]==“Q”| | playerName[players]==“Q”)//检查退出值
{
打破
}
Console.WriteLine(“输入玩家的分数…”);//获取玩家分数
playerScore[players]=Convert.ToDouble(Console.ReadLine());
总分+=球员核心[球员];
players++;//建立数组大小
}虽然(正确);
返回0;
}
公共静态双显示器播放器数据(参考字符串[]播放器名称,参考双[]播放器核心,参考整数播放器)
{
对于(int w=0;w”);
while(z
}

取而代之

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

您像使用and array一样使用它并传递数组,但是方法声明需要方括号。

在调用
InputData
时,在
playerName
之前删除
[]
。您正在传递的其他数组没有它。。为什么那个特别?另外-不知道为什么会要求你通过引用来具体传递它们。。您似乎没有在方法中重新分配数组(我可以看到)。。所以没有必要。现在有人告诉我,“Program.InputData(ref string[],ref double[],ref int,ref double)”有一些无效参数……为什么到处都使用
ref
?我不明白你的意思。我对C#编程相当陌生。但是有人告诉我,当通过引用传递数据时,必须使用ref关键字来通过引用传递数据的内存位置。该死的。我知道这会很愚蠢…非常感谢
public static double DisplayBelowAverage(ref string[] playerName, ref double[] playerScore, ref double averageScore, ref int players){