我正在寻找内联注释步骤的解决方案...我能够运行它,在控制台中输入文本并看到它附加到 0 索引值,但控制台输入也不遵循预期的 for 循环- 非常感谢所有帮助!
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();
}
}
我已经尝试过这段代码并在控制台中运行它,输入文本,并看到一些附加到 0 索引,但没有看到它级联到其他数组索引。
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();
}
}
这接受一个用户输入并将其添加到棒球团队数组内的每个元素。
您还需要将
foreach
循环移到 for
循环之外,否则您将打印数组 4 次。