如何使用 C# 对齐屏幕截图所示的输出?
How to align the output as shown in screenshot using C#?
作为 C# 的新手,我有一些 C# 代码。
该程序是一种日常省钱算法!
我正在努力以某种对齐方式生成输出,如下图所示:
我写的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RemainingDaysCalculation
{
internal class DailyMoneySavingMultiplier
{
public static void Main()
{
DateTime currentDate = DateTime.Now;
int daysInYear = DateTime.IsLeapYear(currentDate.Year) ? 366 : 365;
int daysLeftInYear = daysInYear - currentDate.DayOfYear; // Result is in range 0-365.
int finisheddaysCount = daysInYear - daysLeftInYear;
Console.WriteLine("daysLeftInYear is {0}", daysLeftInYear);
Console.WriteLine("finishedDaysCount is {0}",finisheddaysCount);
int savings = (daysInYear * (daysInYear + 1)) - (finisheddaysCount * (finisheddaysCount -1));
Console.WriteLine(savings);
//Case 2:
Console.WriteLine("_________________________________");
Console.WriteLine(" Day No || Daily Saving || Total Saved");
for (int i=1; i <= daysInYear; i++)
{
Console.WriteLine("{0,-10} || {1,-10} || {2,5}", i, (i * 2), (i * (i + 1)));
//Console.Write(i);
//Console.Write(i * 2);
//Console.Write(i * (i + 1));
}
Console.WriteLine("_________________________________");
}
}
}
输出:
更新:
我已经尝试了此 reference, but i'm getting the exception like
中给定解决方法的 2 种方法
感谢@Oliver 和@Oleg 为我指明了正确的方向并提供了帮助参考。
最后,我使用下面的代码解决了这个问题:
using ConsoleTables;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RemainingDaysCalculation
{
internal class DailyMoneySavingMultiplier
{
public static void Main()
{
DateTime currentDate = DateTime.Now;
int daysInYear = DateTime.IsLeapYear(currentDate.Year) ? 366 : 365;
int daysLeftInYear = daysInYear - currentDate.DayOfYear; // Result is in range 0-365.
int finisheddaysCount = daysInYear - daysLeftInYear;
Console.WriteLine("daysLeftInYear is {0}", daysLeftInYear);
Console.WriteLine("finishedDaysCount is {0}",finisheddaysCount);
int savings = (daysInYear * (daysInYear + 1)) - (finisheddaysCount * (finisheddaysCount -1));
Console.WriteLine("Saving Value " +savings);
//Case 2:
Console.WriteLine("_________________________________");
var table = new ConsoleTable("Day No", "Daily Saving", "Total Saved");
for (int i = 1; i <= daysInYear; i++)
{
table.AddRow(i, (i * 2), (i * (i + 1)));
}
table.Write();
Console.ReadKey();
}
}
}
结果:
作为 C# 的新手,我有一些 C# 代码。
该程序是一种日常省钱算法!
我正在努力以某种对齐方式生成输出,如下图所示:
我写的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RemainingDaysCalculation
{
internal class DailyMoneySavingMultiplier
{
public static void Main()
{
DateTime currentDate = DateTime.Now;
int daysInYear = DateTime.IsLeapYear(currentDate.Year) ? 366 : 365;
int daysLeftInYear = daysInYear - currentDate.DayOfYear; // Result is in range 0-365.
int finisheddaysCount = daysInYear - daysLeftInYear;
Console.WriteLine("daysLeftInYear is {0}", daysLeftInYear);
Console.WriteLine("finishedDaysCount is {0}",finisheddaysCount);
int savings = (daysInYear * (daysInYear + 1)) - (finisheddaysCount * (finisheddaysCount -1));
Console.WriteLine(savings);
//Case 2:
Console.WriteLine("_________________________________");
Console.WriteLine(" Day No || Daily Saving || Total Saved");
for (int i=1; i <= daysInYear; i++)
{
Console.WriteLine("{0,-10} || {1,-10} || {2,5}", i, (i * 2), (i * (i + 1)));
//Console.Write(i);
//Console.Write(i * 2);
//Console.Write(i * (i + 1));
}
Console.WriteLine("_________________________________");
}
}
}
输出:
更新:
我已经尝试了此 reference, but i'm getting the exception like
感谢@Oliver 和@Oleg 为我指明了正确的方向并提供了帮助参考。
最后,我使用下面的代码解决了这个问题:
using ConsoleTables;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RemainingDaysCalculation
{
internal class DailyMoneySavingMultiplier
{
public static void Main()
{
DateTime currentDate = DateTime.Now;
int daysInYear = DateTime.IsLeapYear(currentDate.Year) ? 366 : 365;
int daysLeftInYear = daysInYear - currentDate.DayOfYear; // Result is in range 0-365.
int finisheddaysCount = daysInYear - daysLeftInYear;
Console.WriteLine("daysLeftInYear is {0}", daysLeftInYear);
Console.WriteLine("finishedDaysCount is {0}",finisheddaysCount);
int savings = (daysInYear * (daysInYear + 1)) - (finisheddaysCount * (finisheddaysCount -1));
Console.WriteLine("Saving Value " +savings);
//Case 2:
Console.WriteLine("_________________________________");
var table = new ConsoleTable("Day No", "Daily Saving", "Total Saved");
for (int i = 1; i <= daysInYear; i++)
{
table.AddRow(i, (i * 2), (i * (i + 1)));
}
table.Write();
Console.ReadKey();
}
}
}
结果: