我正在尝试创建一个应用程序,提示用户输入 5 个名称,然后显示每个名称并允许用户输入该特定名称的分数。因此,如果在第一个数组中,索引 [0] 的值是字符串“Bob”,那么在另一个分数数组中,索引 [0] 应该是 bob 的分数。
我很难理解如何将 nameArray[] 传递给 PopulateScore() 方法,以便它可以显示名称供用户输入相应的分数。
我还必须按名称搜索数组并返回分数。
谢谢你的帮助。
public class InitArraya
{
public static string[] arrayName = new string[5];
public static int[] arrayScore = new int[5];
public static void PopulateNameArray()
{
// Prompt user for names and assign values to the elements of the array
for (int intCounter = 1; intCounter < arrayName.Length; intCounter++)
{
Console.Write("Enter name {0}: ", intCounter);
arrayName[intCounter] = Console.ReadLine();
}
}
public static void PopulateScoreArray(string[] array)
{
// Prompt user for names and assign values to the elements of the array
for (int intCounter = 1; intCounter < 5; intCounter++)
{
Console.Write("Enter score for {0}: ", arrayName[0]);
arrayScore[intCounter] = Convert.ToInt32(Console.ReadLine());
}
}
public static void Main( string[] args )
{
Console.WriteLine("Enter 5 names:"); // headings
PopulateNameArray();
PopulateScoreArray(arrayName);
Console.ReadLine();
}
}