简体   繁体   English

C# 基础知识,从文件中读取,通过写入行进行输入,保存排序,控制台应用程序

[英]C# basics, read from files, make input via write line, save sort, Console application

I've only been learning for a few weeks and I'm still at the beginning.我才学了几个星期,还处于起步阶段。 To practice I wrote a small program that prompts for a score and a name, then compares it and gives feedback.为了练习,我编写了一个小程序,提示输入分数和名称,然后进行比较并给出反馈。 see here:看这里:

static int highscore = 1000;
        static void Main(string[] args)
        {

            ///////////////////////////////////
            Console.WriteLine("\n\nDer aktuelle Highscor ist:   {0} \n\n ", highscore);


            Console.Write("\nBitte gib Deinen Score ein:\t");
            int score = Convert.ToInt32(Console.ReadLine());

            Console.Write("\nBitte gib Deinen Namen  ein:\t");
            string spielerName = Console.ReadLine();


            string highscoreSpielerName;


            if (score <= highscore)
            {
                Console.WriteLine("\n\nPech gehabt {0}, Du hast keinen neuen Highscore.", spielerName);

            }

            else if (score > highscore)
            {
                int newHigsore = highscore + score - 1000;
                int divHighscore = score - 1000;
                
                Console.Clear();
                Console.WriteLine("\n\nHerzlichen Glückwunsch, der neue Highscore ist {0} Punkte !! ", newHigsore);
                Console.WriteLine("\n\nDer alte Highscore war {0} Pnkte und wurde um {1} Punkte übertroffen.", highscore, divHighscore);
                Console.WriteLine("\n\n{0} hät den neuen Highscore mit {1} Punkten !", spielerName, newHigsore);
            }

            Console.ReadKey();
        }
           
    }

Now I want to extend it as follows: The input should be stored in a file (array,list), sorted by the highest score, then the following output should be made:现在我想将它扩展如下:输入应该存储在一个文件(数组,列表)中,按最高分排序,然后应该制作以下output:

You are among the top-ten, output whole list with ranking.你在前十名中,output 整个榜单都有排名。

Or或者

Bad luck, you have no new highscore.运气不好,你没有新的高分。

Since I don't know much about arrays, saving and overwriting, I'm stuck here.由于我对arrays了解不多,保存和覆盖,我就卡在这里了。 Would be nice if you could help me here.如果你能在这里帮助我就好了。 This is my approach so far:到目前为止,这是我的方法:

 static void Main(string[] args)
        {
              String[] zeilen = File.ReadAllLines(@"F:\visual Projekte\NeuDatenLesen\bin\Testdaten\Gewinner.CSV");

            foreach (string zeile in zeilen)
            {

                string[] daten = zeile.Split(';');
                double rang = double.Parse(daten[0]);
                string name = daten[1];
                double punkte = double.Parse(daten[2]);

                Console.WriteLine("Platz {0}\t{1}\tPunkte {2}", rang, name, punkte);

                Console.ReadLine();

You may want to use objects.您可能想要使用对象。 Once objects are created, tools for Lists can be used.创建对象后,可以使用列表工具。 Included here is a nugget for Newtonsoft.Json (used to serialize for file) Also the use of Linq.这里包括一个用于 Newtonsoft.Json(用于序列化文件)的块以及 Linq 的使用。

    public class Scores
    {
        public string Name { get; set; }
        public int Score { get; set; }
    }
   class Program
    {
        static void Main(string[] args)
        {
            var ScoreList = new List<Scores>();
            ScoreList.Add(new Scores() { Name = "Alpha", Score = 98324 });
            ScoreList.Add(new Scores() { Name = "Bravo", Score = 88099 });
            ScoreList.Add(new Scores() { Name = "Charlie", Score = 12012 });
            ScoreList.Add(new Scores() { Name = "Delta", Score = 76590 });
            ScoreList.Add(new Scores() { Name = "Echo", Score = 87199 });
            ScoreList.Add(new Scores() { Name = "Foxtrot", Score = 99832 });
            ScoreList.Add(new Scores() { Name = "Golf", Score = 20204 });
            ScoreList.Add(new Scores() { Name = "Hotel", Score = 37209 });
            ScoreList.Add(new Scores() { Name = "India", Score = 08732 });
            ScoreList.Add(new Scores() { Name = "Juliett", Score = 78782 });
            ScoreList.Add(new Scores() { Name = "Kilo", Score = 99120 });
            ScoreList.Add(new Scores() { Name = "Lima", Score = 76555 });
            var saveScores = JsonConvert.SerializeObject(ScoreList);
            File.WriteAllText(@"C:\work\Scores.json", saveScores);
            // read existin file
            var fileRead= File.ReadAllText(@"C:\work\Scores.json");

            List<Scores> existingScores = JsonConvert.DeserializeObject<List<Scores>>(fileRead);

            var highScores = existingScores.OrderByDescending(o => o.Score).Take(10).ToList();

        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在c#console应用程序中读取多行作为输入 - Read multiple line as input in c# console application 如何从控制台应用程序(C#)读/写到SQL数据库 - How to Read/Write to SQL database from console application (C#) C#从控制台应用程序读取/写入流异步 - C# Read/write stream async from console application 从 C# 控制台应用程序静默读/写到 a.cmd 脚本 - Silently Read/Write to a .cmd script from a C# console application C#控制台应用程序不会从命令行读取,因为它应该 - C# Console Application Does NOT read from the command line, as it should 输出/日志Console.Read&Console。从Console应用程序写入TXT / XML文件C# - Output/Log Console.Read & Console.Write from a Console application to a TXT/XML File C# C#控制台应用程序-如何始终从控制台读取输入? - C# Console Application - How do I always read input from the console? 从控制台应用程序输入到另一个C#控制台应用程序? - input from console application to another c# console application? 如何在c#控制台应用程序中永久保存用户提供的输入? - How to permanently save input given by user in c# console application? C#控制台:如何从命令行读取管道输入 - c# console: how can i read piped input from the command line
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM