In C#, I am trying to display a string of text from my last WriteLine method. Getting compiler error

Viewed 46
    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"));
                }
            }
        }
    }

How do I get to output the string for my final WriteLine method By using my teamBA variable to hold the value of hits / atBats? Visual Studio is saying that teamBA is not declared in this scope. And the two arrays hits and atBats are registering an error of syntax error ; value expected. Everything works fine except the last WriteLine method.

1 Answers

In

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"));
}
  • what is the purpose of the 2nd block, it's not in your for loop.
  • you've redeclared a 2nd time float teamBA, the first time is in the top of your method
  • hits[] / atBats[] mean nothing, you can't divide array. You must put an index like hits[2] / atBats[2]
Related