[英]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.