【问题标题】:Sorting correct scores to correct names (Parallel Arrays) C#将正确的分数排序为正确的名称(并行数组)C#
【发布时间】:2018-05-14 21:30:59
【问题描述】:

如上所述,我正在尝试使用并行数组向正确的用户显示正确的分数,如果学生的分数低于 40,它应该显示学生的姓名和他们的分数。

目前我已经这样做了,但它没有将分数分配给正确的名称。

代码:

  static void Main(string[] args)
    {
        //bool YesNo = Console.ReadKey;
        // Empty Variable arrays declared for usage.
        int[] classScores = new int[5];
        string[] studentNames = new string[5];
        object[] ClassANDStudent = new object[5];

        //
        //   if (YesNo == true)
        //Loop Method for user input for student name.
        for (int i = 0; i < studentNames.Length; i++)
        {
            Console.WriteLine("Please enter a Student Name.");
            studentNames[i] = Console.ReadLine();

            Console.WriteLine("Please enter marks.");
            classScores[i] = Convert.ToInt32(Console.ReadLine());

        }
        //


        var Lessthan40Array = classScores.Where(OverallMark => OverallMark <= 40).ToArray();

        Array.Sort(studentNames, classScores, 0, studentNames.Length);
        Array.Sort(studentNames, Lessthan40Array, 0, Lessthan40Array.Length);

        for (int i = 0; i < studentNames.Length; i++)
        {
            ClassANDStudent[i] = studentNames[i] + " " + classScores[i];
        }



        //
       for (int i = 0; i < Lessthan40Array.Length; i++)
        {

            Console.WriteLine(studentNames[i] + " " + Lessthan40Array[i]);
          //  Console.WriteLine(" {0,-10}: {1}", studentNames[i], Lessthan40Array[i]);
        }

        Console.ReadKey();
    }

【问题讨论】:

  • 有什么理由使用并行数组而不是简单的类来存储相关数据?
  • 它通常是使用数组的一部分,所以我需要能够做到这一点我自己学到了很多,只是无法将它分类到相关字段。 (基本上是给定的任务)
  • 您需要排序吗?还是您只需要显示数据? (标题提到排序,但描述只提到显​​示分数
  • 抱歉显示它们。
  • for (int i = 0; i &lt; studentNames.Length; i++) if (classScores[i] &lt; 40) Console.WriteLine("Student " + studentNames[i] + " only scored " + classScores[i]);

标签: c# arrays sorting


【解决方案1】:

您真正需要做的就是遍历所有分数,并为分数小于 40 的任何索引输出学生信息(来自两个数组)。上面代码的问题是,一旦您排序数组之一,它们不再同步。

当我尝试从用户那里获取数字时,我通常做的第一件事就是创建一个辅助方法,该方法将进行一些验证(使用int.TryParse)以确保它们实际上输入了一个整数。此方法将字符串作为提示显示给用户,并一直提示他们,直到他们输入有效数字:

private static int GetIntFromUser(string prompt)
{
    int input;

    do
    {
        Console.Write(prompt);
    } while (!int.TryParse(Console.ReadLine(), out input));

    return input;
}

除此之外,下面的示例代码将提示用户输入他们想要输入的学生数量,然后将每个学生的姓名和分数收集到两个单独的数组中。然后它只是使用一个计数器循环遍历分数数组,并为任何得分低于 40 的人输出学生姓名和分数:

private static void Main()
{
    int numStudents = GetIntFromUser("Enter the number of students: ");

    int[] scores = new int[numStudents];
    string[] names = new string[numStudents];

    for (int i = 0; i < numStudents; i++)
    {
        Console.WriteLine("\nStudent #" + (i + 1));
        Console.Write(" - Enter student's name: ");
        names[i] = Console.ReadLine();
        scores[i] = GetIntFromUser(" - Enter student's score: ");
    }

    Console.WriteLine("\nHere are the students who scored less than 40:");

    for (int i = 0; i < numStudents; i++)
    {
        if (scores[i] < 40)
        {
            Console.WriteLine(" - " + names[i] + " scored " + scores[i]);
        }
    }

    GetKeyFromUser("\nDone! Press any key to exit...");
}

输出

【讨论】:

    【解决方案2】:

    如果你想在写分数之前实际排序:

    static void Main(string[] args)
    {
        int[] classScores = new int[]{30,50,25,39,62};
        string[] studentNames = new string[]{"Jim","John","Mary","Peter","Sarah"};
            Array.Sort(classScores, studentNames);   // sort both according to scores
        for (int i = 0; i < classScores.Length; i++)
        {
            if (classScores[i] < 40)
            {
                Console.WriteLine(classScores[i] + " " + studentNames[i]);
            }
        }
    }
    

    如果您有超过 2 个数组,请创建一个索引为 0 到 length-1 的数组:

    static void Main(string[] args)
    {
        int[] classScores = new int[]{30,50,25,39,62};
        string[] firstNames = new string[]{"Jim","John","Mary","Peter","Sarah"};
        string[] lastNames = new string[]{"Thorpe","Smith","Jones","Tork","Conner"};
        int[] index = new int[]{0,1,2,3,4};
            Array.Sort(classScores, index);   // sort both according to scores
        for (int i = 0; i < classScores.Length; i++)
        {
            if (classScores[i] < 40)
            {
                Console.WriteLine(classScores[i] + " " +
                    firstNames[index[i]] + " " + lastNames[index[i]]);
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多