【发布时间】:2015-02-01 20:55:03
【问题描述】:
我正在 c# 控制台中制作俄罗斯方块游戏。我已经完成了大部分游戏,但我被文件处理困住了。我还没有真正找到任何与此相关的东西,所以我想我可以试一试并询问它。 所以我要做的是将玩家的姓名和分数保存为 NAME:SCORE 的 txt 文件,然后以某种方式按分数对其进行排序,然后将前十名打印为记分牌。 这是我走了多远:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace scoreb
{
class Program
{
private static Random _random = new Random();
private static ConsoleColor GetRandomConsoleColor()
{
var consoleColors = Enum.GetValues(typeof(ConsoleColor));
return (ConsoleColor)consoleColors.GetValue(_random.Next(consoleColors.Length));
}
static void Main(string[] args)
{
int n = 10;
string[] names;
string[] name = new string[n];
string[] score = new string[n];
int i = 0;
while(name[i] != "*"){
Console.WriteLine("Supply your name please!!");
name[i] = Convert.ToString(Console.ReadLine());
Console.WriteLine("Give your score!");
score[i] = Convert.ToString(Console.ReadLine());
Console.WriteLine(name[i] + score[i]);
names = new string[] { name[i] + " " + score[i] };
i++;
}
Console.ReadLine();
//Printout
Console.Clear();
Console.WriteLine();
Console.ForegroundColor = GetRandomConsoleColor();
Console.WriteLine(" *****************************");
Console.ForegroundColor = GetRandomConsoleColor();
Console.WriteLine(" HIGH SCORES");
Console.ForegroundColor = GetRandomConsoleColor();
Console.WriteLine(" *****************************");
Console.WriteLine();
string[] lines = File.ReadAllLines("C:\\asd.txt");
Array.Sort(lines);
string read = null;
StreamReader b = File.OpenText("C:/asd.txt");
Console.WriteLine(" Név Pont");
while ((read = b.ReadLine()) != null)
{
int j = 0;
Console.WriteLine();
Console.ForegroundColor = GetRandomConsoleColor();
Console.WriteLine(" " + (j + 1) + ". " + lines[j]);
Console.WriteLine();
j++;
}
b.Close();
Console.ReadLine();
}
}
}
【问题讨论】: