将数组传递给函数 [英] Passing an array to a function

查看:133
本文介绍了将数组传递给函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我正在尝试编写此程序的问题。我首先在主要内部编写了程序。然后我试图将程序划分为单独的函数。我遇到的更具体的问题发生在第一个函数的开头。第一个大括号提示我这个错误:类,结构或接口成员减速中的无效标记'{'。任何有关这方面的帮助将不胜感激。





Hello,

I am having a problem with this program that I am trying to write. I first wrote the program within the main. Then I attempted to divide the program into separate functions. The more specific problem that I am having occurs right at the beginning of the first function. The first curly brace prompts me with this Error: Invalid Token '{' in class, struct, or interface member deceleration. Any help with this would be greatly appreciated.


namespace VS_Console_Sand_Box_2
{
    class Program
    {
        static void Main(string[] args)
        {
            //declare variables
            string endLoop;
            int totalScore = 0, avgScore = 0, i = 0, count = 0;
            string[] names = new string[100];
            int[] scores = new int[100];

            //Jumpt to module 1
            InPutData(names, scores, ref count);

            //Jump to module 2
            DisplayPlayerData(names, scores, ref count);

            //jump to module 3
            CalculateAverageScore(scores, totalScore, count, ref avgScore);

            //jump to module 4
            DisplayBelowAverage(names, scores, avgScore, count);


            Console.ReadLine();
        }

        //module 1 get user input
        public static void InPutData(string[] names, int[] scores, ref int count);
        { // <-- Problem starts here...            
             do
                {
                    for (i = 0; i < 100; ++i)
                    {
                        Console.Write("Enter Player Name (Q to quit): ");
                        names[i] = Convert.ToString(Console.ReadLine());

                        if (names[i] == "Q")
                        {
                            break;
                        }

                        Console.Write("Enter Score for {0}: ", names[i]);
                        scores[i] = Convert.ToInt16(Console.ReadLine());
                        count++;
                    }
                }
                while (names[i] != "Q");

    }

        //module 2 display the player name and score
        public static void DisplayPlayerData(string[] names, int[] scores, ref int count);
{
        Console.WriteLine("\n\nName:\t\tScore:");
        for (i = 0; i < count; i++)
        Console.WriteLine("" + names[i] + "\t\t" + scores[i]);
}

        //module 3 calculate the average of all scores
        public static void CalculateAverageScore(int[] scores, int totalScore, int count, ref int avgScore);
{
        foreach (int score in scores)
        totalScore = (totalScore + score);
        avgScore = (totalScore / count);
}

        //module 4 determine and display how many players are below average
        public static void DisplayBelowAverage(string[] names, int[] scores, int avgScore, int count);
{
        Console.WriteLine("\nAverage Score: {0:F2}", avgScore);
        Console.WriteLine("Players who scored below average:\n");

          for (i = 0; i < count; i++)
              if (scores[i] < avgScore)
              {
                    Console.WriteLine("" + names[i] + "\t\t" + scores[i]);
              }
}

    }
}

推荐答案

引用:

public static void InPutData(string [] names,int []得分,ref int count) ;

public static void InPutData(string[] names, int[] scores, ref int count);



删除行尾的分号。


Remove the semicolon at the end of the line.


那是因为你正在使用行终止符(有人告诉我这个字符的真实姓名)你的代码中有; ,这就是为什么编译器不期望 {代码中的令牌并给出了此错误。



请参阅您的代码,



That is because you're using a line terminator (someone tell me the real name of this character) there ; in your code, that is why the compiler was not expecting the { token in the code and gave this error.

See in your code,

public static void InPutData(string[] names, int[] scores, ref int count); // <--
{





删除它,现在就会这样,





Remove it, and it will be like this now,

public static void InPutData(string[] names, int[] scores, ref int count)
{





..现在它不会抱怨。 :-)



此外,毕竟没有错误传递数组,也没有错误。



.. now it won't complain. :-)

Also, there was no error passing array after all and there won't be an error.


这篇关于将数组传递给函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆