简体   繁体   English

位置C#位置文字

[英]Position C# position text

I have just started learning C# coding and my latest assignment question on arrays requires the output shown in example picture, I will put in the code I have written so far. 我刚刚开始学习C#编码,而我对数组的最新赋值问题需要示例图片中所示的输出,我将输入到目前为止编写的代码。 My problem is that when I run the program I have spaces between the lines and the 2 tables don't quite line up, does any one have any idea how I can re position to look like sample pic? 我的问题是,当我运行该程序时,在行与行之间没有空格,而2个表却不太对齐,是否有人知道如何重新定位为示例图片? Thanks! 谢谢!

This is sample output 这是示例输出

在此处输入图片说明

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();

You are using WriteLine which will add an entry in new line, it's better if you combine your output and print: 您正在使用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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM