Commit 807db791 authored by 李静's avatar 李静

解决窗口最小化后再还原时内容会错乱的问题;

优化结果输出逻辑
parent ba6fb711
...@@ -13,11 +13,14 @@ namespace Lottery ...@@ -13,11 +13,14 @@ namespace Lottery
public static void ShowTitle(string title) public static void ShowTitle(string title)
{ {
Console.ForegroundColor = ConsoleColor.Cyan; Console.ForegroundColor = ConsoleColor.Cyan;
Enumerable.Range(0, Console.LargestWindowWidth - 3).ToList().ForEach(p => Console.Write("$")); Enumerable.Range(0, Console.WindowWidth - 3).ToList().ForEach(p => Console.Write("$"));
Console.SetCursorPosition(Console.LargestWindowWidth / 2, 1); Console.Write(Environment.NewLine);
Console.SetCursorPosition(Console.WindowWidth / 2, 1);
title.ToList().ForEach(p => Console.Write(p)); title.ToList().ForEach(p => Console.Write(p));
Console.Write(Environment.NewLine);
Console.SetCursorPosition(0, 2); Console.SetCursorPosition(0, 2);
Enumerable.Range(0, Console.LargestWindowWidth - 3).ToList().ForEach(p => Console.Write("$")); Enumerable.Range(0, Console.WindowWidth - 3).ToList().ForEach(p => Console.Write("$"));
Console.Write(Environment.NewLine);
} }
public static void Show(int i, IReadOnlyList<Person> persons, IReadOnlyList<Lottery> lotteries) public static void Show(int i, IReadOnlyList<Person> persons, IReadOnlyList<Lottery> lotteries)
...@@ -43,61 +46,52 @@ namespace Lottery ...@@ -43,61 +46,52 @@ namespace Lottery
public static void ShowGradeList(IReadOnlyList<Person> person, int highlight) public static void ShowGradeList(IReadOnlyList<Person> person, int highlight)
{ {
Console.Write(Environment.NewLine);
int colCount = 3;
var titles = new List<string> { "排名", "人名", "金币" };
var people = person.OrderByDescending(p => p.NewGrade).ToList(); var people = person.OrderByDescending(p => p.NewGrade).ToList();
int width = ((Console.LargestWindowWidth - 6) / 3 - 6) / 3;
Console.ForegroundColor = ConsoleColor.Yellow; Console.ForegroundColor = ConsoleColor.Yellow;
var titles = new List<string> { "排名", "人名", "金币" };
for (int i = 0; i < titles.Count; i++)
{
Console.SetCursorPosition(i * width, 5);
Console.Write(titles[i]);
}
var list1 = people.Take(people.Count / 3).ToList();
int index = 1;
for (int i = 0; i < list1.Count; i++)
{
Console.ForegroundColor = index <= highlight ? ConsoleColor.Yellow : ConsoleColor.White;
Console.SetCursorPosition(0, i + 6);
Console.Write($"{index++}");
Console.SetCursorPosition(width, i + 6);
Console.Write($"{list1[i].Name}");
Console.SetCursorPosition(width * 2, i + 6); //将结果分为多列输出,每列间隔
Console.Write($"{list1[i].NewGrade}"); int colGap = (int)(Console.WindowWidth * 0.05);
} //每列宽度
Console.ForegroundColor = ConsoleColor.Yellow; int colWidth = (Console.WindowWidth - colGap * (colCount-1)) / colCount;
for (int i = 1; i <= titles.Count; i++) //每列中分为多个表头,每个表头的宽度
{ int titleWidth = colWidth / titles.Count();
Console.SetCursorPosition((i - 1) * width + width * 4 - 4, 5);
Console.Write(titles[i - 1]); //添加表头
} for (int colIndex = 0; colIndex < colCount; colIndex++)
var list2 = people.Skip(people.Count / 3).Take(people.Count / 3).ToList();
for (int i = 0; i < list2.Count; i++)
{
Console.ForegroundColor = index <= highlight ? ConsoleColor.Yellow : ConsoleColor.White;
Console.SetCursorPosition(0 + width * 4 - 4, i + 6);
Console.Write($"{index++}");
Console.SetCursorPosition(width + width * 4 - 4, i + 6);
Console.Write($"{list2[i].Name}");
Console.SetCursorPosition(width * 2 + width * 4 - 4, i + 6);
Console.Write($"{list2[i].NewGrade}");
}
Console.ForegroundColor = ConsoleColor.Yellow;
for (int i = 1; i <= titles.Count; i++)
{ {
Console.SetCursorPosition((i - 1) * width + width * 8 - 4, 5); for (int i = 0; i < titles.Count; i++)
Console.Write(titles[i - 1]); {
Console.SetCursorPosition((colWidth + colGap) * colIndex + titleWidth * i, 4);
Console.Write(titles[i]);
}
} }
var list3 = people.Skip(people.Count / 3).Skip(people.Count / 3).ToList(); Console.Write(Environment.NewLine);
for (int i = 0; i < list3.Count; i++)
//按照从上到下、从左到右的顺序分多列输出结果
int rowCount = (int)Math.Ceiling((double)person.Count() / colCount);
for (int i = 0; i < people.Count; i++)
{ {
int rowIndex = i % rowCount;
int colIndex = i / rowCount;
int index = i + 1;
Console.ForegroundColor = index <= highlight ? ConsoleColor.Yellow : ConsoleColor.White; Console.ForegroundColor = index <= highlight ? ConsoleColor.Yellow : ConsoleColor.White;
Console.SetCursorPosition(0 + width * 8 - 4, i + 6); Console.SetCursorPosition((colWidth + colGap) * colIndex + 0, rowIndex + 5);
Console.Write($"{index++}"); Console.Write(index);
Console.SetCursorPosition(width + width * 8 - 4, i + 6); Console.SetCursorPosition((colWidth + colGap) * colIndex + titleWidth, rowIndex + 5);
Console.Write($"{list3[i].Name}"); Console.Write(people[i].Name);
Console.SetCursorPosition(width * 2 + width * 8 - 4, i + 6); Console.SetCursorPosition((colWidth + colGap) * colIndex + titleWidth * 2, rowIndex + 5);
Console.Write($"{list3[i].NewGrade}"); Console.Write(people[i].NewGrade);
if (colIndex == colCount - 1 || index == people.Count)
{
//每行的最后一列
Console.Write(Environment.NewLine);
}
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment