如何使用带数组的方法

时间:2013-08-30 17:14:48

标签: c# arrays methods

所以除了最后一项之外,我已经为我的小程序完成了我所需要的一切。我需要创建一个方法,将两个游戏的平均价值数据打印出来并在最后显示。 到目前为止,这是我的代码:

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

namespace Final
{
class FinalProgram
{
    public static void Main()
    {
        //Declaring Arrays for the players, games/avg, and stats.
        double[, ,] stats = new double[3, 2, 10];
        string[] players = new string[3];
        int x, y;

        //Here is my Pre-set Array
        players[0] = "Tom Brady";
        players[1] = "Drew Brees";
        players[2] = "Peyton Manning";
        //Here starts my loops that will ask for first players stats for game 1 first,        then loop to game 2. After that finishes it will loop to player 2, etc.
        for (x = 0; x < 3; ++x)
        {
            Console.WriteLine("Enter stats for {0}", players[x]);
            for (y = 0; y < 2; ++y)
            {
                //Here starts my writeable array. This was to save from having to ask the question about stats 60 times in code.
                Console.WriteLine("Game {0}", y + 1);
                Console.WriteLine("Enter pass attempts: ");
                stats[x, y, 0] = Convert.ToDouble(Console.ReadLine());
                /*Console.WriteLine("Enter completions: ");    (Just saving from having to enter a ton of data while testing)
                stats[x, y, 1] = Convert.ToDouble(Console.ReadLine());
                Console.WriteLine("Enter completion percentage: ");
                stats[x, y, 2] = Convert.ToDouble(Console.ReadLine());
                Console.WriteLine("Enter total yards: ");
                stats[x, y, 3] = Convert.ToDouble(Console.ReadLine());
                Console.WriteLine("Enter touchdowns: ");
                stats[x, y, 4] = Convert.ToDouble(Console.ReadLine());
                Console.WriteLine("Enter interceptions: ");
                stats[x, y, 5] = Convert.ToDouble(Console.ReadLine());
                Console.WriteLine("Enter rushing yards: ");
                stats[x, y, 6] = Convert.ToDouble(Console.ReadLine());
                Console.WriteLine("Enter rushing touchdowns: ");
                stats[x, y, 7] = Convert.ToDouble(Console.ReadLine());
                Console.WriteLine("Enter fumbles: ");
                stats[x, y, 8] = Convert.ToDouble(Console.ReadLine());
                Console.WriteLine("Enter QB rating: ");
                stats[x, y, 9] = Convert.ToDouble(Console.ReadLine());*/
            }
        }

        char letter = 'a';
        string input;
        {
            while (letter != 'z' || letter != 'Z')
            {
                Console.Write("Enter the first letter of the first name of a player (T, D, P), enter A for all players, or Z to quit: ");
                input = Console.ReadLine();
                letter = Convert.ToChar(input);
                if (letter == 'a' || letter == 'A')
                {
                    Console.WriteLine("Player             Att   Comp  Comp %  PYards PTD INT RYards  RTD  F  QBR");
                    Console.WriteLine("\n----------------------------------------------------------------------------");
                    Console.WriteLine("{0}  - Avg  - {1}    {2}     {3}      {4}    {5}   {6}   {7}     {8}   {9}  {10}", players[0], stats[0, 1, 0], stats[0, 1, 1], stats[0, 1, 2].ToString("P"), stats[0, 1, 3], stats[0, 1, 4], stats[0, 1, 5], stats[0, 1, 6], stats[0, 1, 7], stats[0, 1, 8], stats[0, 1, 9].ToString("P"));
                    Console.WriteLine("\n----------------------------------------------------------------------------");
                    Console.WriteLine("{0}  - Avg  - {1}    {2}     {3}      {4}    {5}   {6}   {7}     {8}   {9}  {10}", players[1], stats[1, 1, 0], stats[1, 1, 1], stats[1, 1, 2].ToString("P"), stats[1, 1, 3], stats[1, 1, 4], stats[1, 1, 5], stats[1, 1, 6], stats[1, 1, 7], stats[1, 1, 8], stats[1, 1, 9].ToString("P"));
                    Console.WriteLine("\n----------------------------------------------------------------------------");
                    Console.WriteLine("{0}- Avg- {1}    {2}     {3}      {4}    {5}   {6}   {7}     {8}   {9}  {10}", players[2], stats[2, 1, 0], stats[2, 1, 1], stats[2, 1, 2].ToString("P"), stats[2, 1, 3], stats[2, 1, 4], stats[2, 1, 5], stats[2, 1, 6], stats[2, 1, 7], stats[2, 1, 8], stats[2, 1, 9].ToString("P"));
                }
                else if (letter == 't' || letter == 'T')
                {
                    Console.WriteLine("\n----------------------------------------------------------------------------");
                    Console.WriteLine("Player             Att   Comp  Comp %  PYards PTD INT RYards  RTD  F  QBR");
                    Console.WriteLine("\n----------------------------------------------------------------------------");
                    Console.WriteLine("{0}  - Avg  - {1}    {2}     {3}      {4}    {5}   {6}   {7}     {8}   {9}  {10}", players[0], stats[0, 1, 0], stats[0, 1, 1], stats[0, 1, 2].ToString("P"), stats[0, 1, 3], stats[0, 1, 4], stats[0, 1, 5], stats[0, 1, 6], stats[0, 1, 7], stats[0, 1, 8], stats[0, 1, 9].ToString("P"));
                }
                else if (letter == 'd' || letter == 'D')
                {
                    Console.WriteLine("\n----------------------------------------------------------------------------");
                    Console.WriteLine("Player             Att   Comp  Comp %  PYards PTD INT RYards  RTD  F  QBR");
                    Console.WriteLine("\n----------------------------------------------------------------------------");
                    Console.WriteLine("{0}  - Avg  - {1}    {2}     {3}      {4}    {5}   {6}   {7}     {8}   {9}  {10}", players[1], stats[1, 1, 0], stats[1, 1, 1], stats[1, 1, 2].ToString("P"), stats[1, 1, 3], stats[1, 1, 4], stats[1, 1, 5], stats[1, 1, 6], stats[1, 1, 7], stats[1, 1, 8], stats[1, 1, 9].ToString("P"));
                }
                else if (letter == 'p' || letter == 'P')
                {
                    Console.WriteLine("\n----------------------------------------------------------------------------");
                    Console.WriteLine("Player             Att   Comp  Comp %  PYards PTD INT RYards  RTD  F  QBR");
                    Console.WriteLine("\n----------------------------------------------------------------------------");
                    Console.WriteLine("{0}- Avg- {1}    {2}     {3}      {4}    {5}   {6}   {7}     {8}   {9}  {10}", players[2], stats[2, 1, 0], stats[2, 1, 1], stats[2, 1, 2].ToString("P"), stats[2, 1, 3], stats[2, 1, 4], stats[2, 1, 5], stats[2, 1, 6], stats[2, 1, 7], stats[2, 1, 8], stats[2, 1, 9].ToString("P"));
                }
                else
                {
                    Console.WriteLine("Sorry, not a valid initial.");
                    Console.Write("Enter next players initial or Z to quit ");
                    input = Console.ReadLine();
                    letter = Convert.ToChar(input);
                }

            }
        }

    }

}

}

所以基本上我现在就设置打印出为每个玩家输入的统计数据。我需要将显示“游戏”的打印状态更改为显示输入的两个“游戏”的平均值,并且我不确定如何执行此操作。

如果有人可以帮助或至少让我朝着正确的方向前进,我会非常感激。我还想提一下,我是一个非常初学的编码器,所以我不打算用我所拥有的东西压缩或做任何事情,只需添加最后一个函数。我需要使用一种方法来实现它。

谢谢!

1 个答案:

答案 0 :(得分:0)

public double[,] GetAveragesOfThirdDimension(double[,,] input)
{
    double[,] averages = new double[input.GetLength(0), input.GetLength(1)];
    for (int i = 0; i < input.GetLength(0); i++)
    {
        for (int j = 0; j < input.GetLength(1); j++)
        {
            for (int k = 0; k < input.GetLength(2); k++)
            {
                averages[i,j] = averages[i,j] + input[i,j,k];
            }
            averages[i,j] = averages[i,j] / input.GetLength(2);
        }
    }
    return averages;
}