简体   繁体   中英

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. 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? 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:

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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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