我刚刚开始学习 C# 编码,我最新的数组作业问题需要示例图片中所示的输出,我将输入到目前为止我编写的代码。我的问题是,当我运行程序时,行之间有空格,并且两个表格没有完全对齐,有人知道如何重新定位以看起来像示例图片吗?谢谢!
这是示例输出
int[] hrs = { 8, 24, 9, 7, 6, 12, 10, 11, 23, 1, 2, 9, 8, 8, 9, 7, 9, 15, 6, 1, 7, 6, 12, 10, 11, 23, 1, 2, 9, 8 };
decimal fee;
const decimal HOURLY_RATE = 2.5m, MAX_FEE = 20;
decimal avg = 0;
decimal total = 0;
Console.WriteLine("Hours Parking Fee");
for (int count = 0; count < hrs.Length; count++)
{
Console.WriteLine("{0, 3}", hrs[count]);
fee = hrs[count] * HOURLY_RATE;
if (fee > MAX_FEE)
{
fee = MAX_FEE;
}
Console.WriteLine("{0, 13}", fee.ToString("C"));
// calculate average fee paid
{
total = total + fee;
}
}
avg = total / 30; //average = total / 30;
Console.WriteLine("average parking fee: " + avg.ToString("C"));
Console.ReadKey();
Console.ReadKey();
最佳答案
您正在使用 WriteLine
它将在新行中添加一个条目,如果您将输出和打印结合起来会更好:
for (int count = 0; count < hrs.Length; count++)
{
//Console.WriteLine("{0,3}", hrs[count]);
fee = hrs[count] * HOURLY_RATE;
if (fee > MAX_FEE)
{
fee = MAX_FEE;
}
//Console.Write("{0,13}", fee.ToString("C"));
Console.WriteLine("{0,3} {1,13}", hrs[count], fee.ToString("C"));
// calculate average fee paid
{
total = total + fee;
}
}
关于c# - 位置 C# 位置文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42849833/