-1

我正在尝试创建一个应用程序,提示用户输入 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();
    }
}
4

3 回答 3

1

制作包含名称和分数的对象数组,这将使您的解决方案更加有用和可读。

public class NameScore{
   public string Name { get; set; }
   public int Score { get; set; }
}

public class InitArraya{
    public NameScore[] arrayScore = new NameScore[5]; 
...
于 2012-11-01T09:20:33.270 回答
1
public static void PopulateScoreArray(string[] array)
{

    // Prompt user for names and assign values to the elements of the array
    for (int intCounter = 0; intCounter < array.Length; intCounter++)
    {
        Console.Write("Enter score for {0}: ", array[intCounter]);
        arrayScore[intCounter] = Convert.ToInt32(Console.ReadLine());

    }
}

假设arrayName中总是有5个名字。否则应进行额外检查。

哦,并且在 PopulateNameArray 中也从 0 开始 intCounter。

于 2012-11-01T09:20:42.123 回答
0

public static void PopulateScoreArray(string[] array)

改变

 Console.Write("Enter score for {0}: ", arrayName[0]);

Console.Write("Enter score for {0}: ", array[intCounter]);

使用输入数组。同样 for(-) 将计数器的开始更改为 0

for (int intCounter = 0; intCounter < 5; intCounter++)
于 2012-11-01T09:19:43.560 回答