获得最好的

时间:2012-07-07 10:26:11

标签: visual-studio-2010

我是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.");

1 个答案:

答案 0 :(得分:1)

有很多方法可以做到这一点,但为了让您入门,我已经为您的代码添加了一些可以使用的部分。

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

得分与人名作为关键词:

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();