В C# я пытаюсь отобразить строку текста из моего последнего метода WriteLine. Ошибка компиляции - PullRequest
2 голосов
/ 04 марта 2020
    using System;
    using static System.Console;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    //My Baseball application named BatStat
    namespace BatStat
    {
        class Program
        {
            static void Main()
            {
                //My declared variables that hold the user input in memory
                string teamName;
                string[] playerName = new string[9];
                string[] playerPosition = new string[9];
                int[] atBats = new int[9];
                int[] hits = new int[9];
                float teamBA = 0;

                //Begin program by requesting user to enter team name value
                WriteLine("Welcome to Bat Stat, enter team name");
                teamName = Console.ReadLine();

                //Traverse all arrays and accumalate values from user input
                for (int i = 0; i < playerName.Length; i++)
                {
                    WriteLine("Enter players name");
                    playerName[i] = ReadLine();
                    WriteLine("Enter Players Position");
                    playerPosition[i] = ReadLine();
                    WriteLine("Enter Players at bats");
                    atBats[i] = Convert.ToInt32(ReadLine());
                    WriteLine("Enter Players hits");
                    hits[i] = Convert.ToInt32(ReadLine());
                }

                //Display top row menu for Bat Stat table
                WriteLine("{0, -10}{1, -10}{2, -5}{3, -5}{4, -5}{5, -5}", "Team", "Player", "Pos", "AB", "H", "BA");

                //for loop to traverse the arrays and display user input data
                for (int x = 0; x < playerName.Length; ++x)
                {
                    float playerBA = (float) hits[x] / atBats[x];
                    WriteLine("{0, -10}{1, -10}{2, -5}{3, -5}{4, -5}{5, -4}", teamName, playerName[x], playerPosition[x], atBats[x].ToString("d"), hits[x].ToString("d"), playerBA.ToString("F3"));
                }
                {
                    //Display the team batting average
                    float teamBA = hits[] / atBats[];
                    WriteLine("The batting average for the {0} is {1}", teamName, teamBA.ToString("F3"));
                }
            }
        }
    }

Как мне получить выходную строку для моего окончательного метода WriteLine Используя мою переменную teamBA для хранения значения hit / atBats? Visual Studio говорит, что teamBA не объявлен в этой области. И два обращения к массивам и atBats регистрируют ошибку синтаксической ошибки; ожидаемое значение Все отлично работает, кроме последнего метода WriteLine.

1 Ответ

2 голосов
/ 04 марта 2020

В

for (int x = 0; x < playerName.Length; ++x)
{
    float playerBA = (float) hits[x] / atBats[x];
    WriteLine("{0, -10}{1, -10}{2, -5}{3, -5}{4, -5}{5, -4}", teamName, playerName[x], playerPosition[x], atBats[x].ToString("d"), hits[x].ToString("d"), playerBA.ToString("F3"));
}
{
    //Display the team batting average
    float teamBA = hits[] / atBats[];
    WriteLine("The batting average for the {0} is {1}", teamName, teamBA.ToString("F3"));
}
  • какова цель 2-го блока, его нет в вашем for l oop.
  • , который вы повторно объявили во второй раз float teamBA, первый раз в начале вашего метода
  • hits[] / atBats[] ничего не значит, вы не можете разделить массив. Вы должны поставить индекс как hits[2] / atBats[2]
...