0

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

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);
                }

            }
        }

    }

}

}

所以基本上我现在已经将它设置为打印出为每个玩家输入的统计数据。我需要将打印的统计数据从显示“游戏”更改为显示输入的两个“游戏”的平均值,但我不确定该怎么做。

如果有人可以帮助或至少将我推向正确的方向,我将不胜感激。我还想提一下,我是一个非常初学者的编码器,所以我不想浓缩或用我所拥有的东西做任何疯狂的事情,只需添加最后一个函数。我需要使用一种方法来做到这一点。

谢谢!

4

1 回答 1

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;
}
于 2013-08-30T18:06:15.820 回答