I'm seeking a solution for the inline commented out steps... I am able to run this, enter text in console and see it append to the 0 index value but the console input is not then also following the expected for loop - all help is much appreciated!
using System;
using System.Collections.Generic;
class Program
{
static void Main(string[] args)
{
// A one-dimensional array of strings.
string[] baseballTeams = { "Mariners", "Giants", "Angels", "Dodgers" };
// Ask the user to input some text.
Console.WriteLine("Please enter some text.");
// A loop that iterates through each string in the array and adds the user's text input to the end of each string.
// This step will not output anything to the console, but will update each array element by appending the user's text.
for (int i = 0; i < baseballTeams.Length; i++)
{
string toArray = baseballTeams[i] + Console.ReadLine();
baseballTeams[i] = toArray;
// Then create a second loop that prints off each string in the array one at a time.
foreach (string team in baseballTeams)
{
Console.WriteLine(team);
}
}
Console.ReadLine();
}
}
I've tried this code and run it in console, entering text, and seeing some append to the 0 index, but am not seeing it cascade out to the other array indexes.
using System;
using System.Collections.Generic;
class Program
{
static void Main(string[] args)
{
// A one-dimensional array of strings.
string[] baseballTeams = { "Mariners", "Giants", "Angels", "Dodgers" };
// Ask the user to input some text.
Console.WriteLine("Please enter some text.");
string userInput = Console.ReadLine();
// A loop that iterates through each string in the array and adds the user's text input to the end of each string.
// This step will not output anything to the console, but will update each array element by appending the user's text.
for (int i = 0; i < baseballTeams.Length; i++)
{
baseballTeams[i] = baseballTeams[i] + userInput;
}
// Then create a second loop that prints off each string in the array one at a time.
foreach (string team in baseballTeams)
{
Console.WriteLine(team);
}
Console.ReadLine();
}
}
This accepts one user input and adds it to each element inside baseballTeams array. If you wanted to have the user input some text for each team you would move string userInput = Console.ReadLine();
inside the for
loop
You also needed to move foreach
loop outside of the for
loop otherwise you would have printed out the array 4 times.