-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

これを行う方法はたくさんありますが、最初に試してみるために、コードにいくつかの部分を追加しました。

各ゲームが終了したら、スコアと名前をコレクションに追加できます。

人の名前をキーとしたスコアのコレクション:

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 に答える