-1

我是 C# 编程的新手,我正在为一个正在创建 3 个问题测试的朋友做一个控制台应用程序。我需要获取前 5 名用户的姓名并显示他们的成绩,但我不知道该怎么做。你能帮我吗谢谢。这是代码:

string Name, yn;
int points = 0;
 do{
        Console.WriteLine("Please enter your fullname here:");
        Name = Console.ReadLine();
        Console.WriteLine(" ");

        Console.WriteLine("Hello " + Name + " Welcome to this simple test.");
        Console.WriteLine(" ");

        Console.WriteLine("1) What is 5 + 6?");
        Console.WriteLine("   a)10");
        Console.WriteLine("   b)30");
        Console.WriteLine("   c)11");
        Console.Write("Answer: ");
        string QAns1 = "C";
        string MyAns1 = Console.ReadLine().ToUpper();
        Console.Clear();
          if (MyAns1 == QAns1)
                {
                    Point++;
                }

        Console.WriteLine("2) What is the first letter of Apple?");
        Console.WriteLine("   a)A");
        Console.WriteLine("   b)c");
        Console.WriteLine("   c)a");
        Console.Write("Answer: ");
        string QAns2 = "A";
        string MyAns2 = Console.ReadLine().ToUpper();
        Console.Clear();
          if (MyAns2 == QAns2)
                {
                    Point++;
                }
       Console.WriteLine("3) What is the plural word of tooth?");
        Console.WriteLine("   a)tentacles");
        Console.WriteLine("   b)Teeth");
        Console.WriteLine("   c)tooths");
        Console.Write("Answer: ");
        string QAns3 = "B";
        string MyAns3 = Console.ReadLine().ToUpper();
        Console.Clear();
          if (MyAns3 == QAns3)
                {
                    Point++;
                }

        Console.WriteLine(" Mr. " + Name + " your final score is " + Point + "/10 ");
        Console.WriteLine(" Do you want to try again? ");
        yn = Console.ReadLine().ToUpper();
  }while (yn== "Y");
     Console.WriteLine("Thank you for using our program.");
4

1 回答 1

1

有很多方法可以做到这一点,但为了让你开始,我在你的代码中添加了一些你可以玩的部分。

每场比赛结束后,您可以将分数和名称添加到集合中。

以人名为key的分数集合:

var playedGames = new Dictionary<string, int>();

然后当每场比赛结束时,您可以像这样将分数添加到集合中:

playedGames.Add(Name, Point);

然后,当没有更多比赛将要进行时,您可以按得分最高的人订购收藏并取出其中的 5 个,如下所示:

var topScorers = playedGames.OrderByDescending(x => x.Value).Take(5);

然后你可以打印出这 5 个顶级球员:

foreach (var topScorer in topScorers)
{
    Console.WriteLine("Congratulations {0} you made the highscore with {1}", 
        topScorer.Key, topScorer.Value);
}

这是有关如何执行此操作的完整示例:

string Name, yn;
int points = 0;
var playedGames = new Dictionary<string, int>();
do
{
    var Point = 0;
    Console.WriteLine("Please enter your fullname here:");
    Name = Console.ReadLine();
    Console.WriteLine(" ");

    Console.WriteLine("Hello " + Name + " Welcome to this simple test.");
    Console.WriteLine(" ");

    Console.WriteLine("1) What is 5 + 6?");
    Console.WriteLine("   a)10");
    Console.WriteLine("   b)30");
    Console.WriteLine("   c)11");
    Console.Write("Answer: ");
    string QAns1 = "C";
    string MyAns1 = Console.ReadLine().ToUpper();
    Console.Clear();
    if (MyAns1 == QAns1)
    {
        Point++;
    }

    Console.WriteLine("2) What is the first letter of Apple?");
    Console.WriteLine("   a)A");
    Console.WriteLine("   b)c");
    Console.WriteLine("   c)a");
    Console.Write("Answer: ");
    string QAns2 = "A";
    string MyAns2 = Console.ReadLine().ToUpper();
    Console.Clear();
    if (MyAns2 == QAns2)
    {
        Point++;
    }
    Console.WriteLine("3) What is the plural word of tooth?");
    Console.WriteLine("   a)tentacles");
    Console.WriteLine("   b)Teeth");
    Console.WriteLine("   c)tooths");
    Console.Write("Answer: ");
    string QAns3 = "B";
    string MyAns3 = Console.ReadLine().ToUpper();
    Console.Clear();
    if (MyAns3 == QAns3)
    {
        Point++;
    }

    playedGames.Add(Name, Point);
    Console.WriteLine(" Mr. " + Name + " your final score is " + Point + "/10 ");
    Console.WriteLine(" Do you want to try again? ");
    yn = Console.ReadLine().ToUpper();
} while (yn == "Y");

var topScorers = playedGames.OrderByDescending(x => x.Value).Take(5);

foreach (var topScorer in topScorers)
{
    Console.WriteLine("Congratulations {0} you made the highscore with {1} in score!",
        topScorer.Key, topScorer.Value);
}
Console.WriteLine("Thank you for using our program.");

Console.ReadLine();
于 2012-07-07T10:35:36.143 回答