好吧,所以我完全迷路了,我从来没有做过任何c#编码,我正在努力学习。
我必须做的任务:
计算每个顾客的停车费,并连同停放的时间一起计算出来。
最高停车费为20美元。
计算并输出停车费用的平均值。
总体任务:将数据文件“hours.txt”读入数据类型整数数组,计算每个客户的停车费,并输出停车时间和停车费。
计算平均停车费,输出平均停车费(格式为小数点2位),声明数组并分配数字,如下所示。
到目前为止,这就是我想出来的。记住,就像我说的,我对这件事很陌生,我正在寻找一些指导。
int[] hours;
hours = new int[30];
const decimal HOURLY_RATE = 2.50m;
const decimal MAX_FEE = 20.00m; //Capped at S20.00
decimal parkFee;
decimal parkingCost = HOURLY_RATE;
int[] hoursArray = { 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 };
Console.WriteLine("Hours " + "Parking Fee ");
int total = 0;
double average = 0;
for (int index = 0; index < hours.Length; index++)
if (parkingCost > MAX_FEE)
{
parkFee = parkingCost * MAX_FEE;
Console.WriteLine("Hours " + "Parking Fee ");
}
average = (double)total / hours.Length;
Console.WriteLine("Average = " + average.ToString("N2"));
Console.ReadKey();`
发布于 2017-04-06 08:53:24
你从来不计算你的费用之前,你检查它是否大于最高费率。因此,您看不到任何结果,因为您的费用从来没有计算(总是0)。另外,您忽略了for循环的一些大括号,因此它不会在for循环下循环您的代码。
快速解决办法:
int[] hours = new int[30]{ 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 };
const decimal HOURLY_RATE = 2.50m;
const decimal MAX_FEE = 20.00m; //Capped at S20.00
Console.WriteLine("Hours and fee's parked per person");
decimal total = 0;
for (int index = 0; index < hours.Length; index++)
{
decimal parkFee = HOURLY_RATE * hours[index];
if (parkFee > MAX_FEE)
parkFee = MAX_FEE;
total += parkFee;
Console.WriteLine("Hours: {0}. Parking Fee: {1}", hours[index], parkFee);
}
decimal average = total / hours.Length;
Console.WriteLine("Average = {0}", average.ToString("N2"));
Console.ReadKey();
我还清理了一些双变量,并将一些变量声明移至计算它们的位置。我还从您的decimal
变量中创建了一个total
值。您使用的是int
,它只会给出整数,这将导致费用总额的不准确。通过使用十进制,您可以解决这个问题。最后,我还将平均值更改为小数,这在本例中似乎更有意义,因为到目前为止,所有变量都使用了十进制。
编辑:,因为您特别要求从文本文件中读取数组。这是如何从文本文件创建hours
数组,而不是在代码中声明它:
const decimal HOURLY_RATE = 2.50m;
const decimal MAX_FEE = 20.00m; //Capped at S20.00
// See http://stackoverflow.com/a/556142/7397065 why you should use @ here
string path = @"C:\your\path\here.txt";
string stringFromFile = File.ReadAllText(path);
// Since your array is delimited on whitespace, use null as parameter
// You also need int's instead of string. So directly convert the results to int.
// We now make a List(), which also gives you more flexibility.
// Want to change your delimiter? See https://msdn.microsoft.com/en-us/library/b873y76a(v=vs.110).aspx
List<int> hours = stringFromFile.Split(null).Select(int.Parse).ToList();
Console.WriteLine("Hours and fee's parked per person");
decimal total = 0;
// Let's change this to a foreach. That's clearer to work with.
foreach (int parkingTime in hours)
{
decimal parkFee = HOURLY_RATE * parkingTime;
if (parkFee > MAX_FEE)
parkFee = MAX_FEE;
total += parkFee;
Console.WriteLine("Hours: {0}. Parking Fee: {1}", parkingTime, parkFee);
}
// We have to use .Count now, since we use List()
decimal average = total / hours.Count;
Console.WriteLine("Average = {0}", average.ToString("N2"));
Console.ReadKey();
发布于 2017-04-06 08:52:57
最简单的解决方案是迭代hoursArray
,并检查每个公园周期、时间、费率和最高收费之间的最低值。然后将其添加到您的总费用中,并将其除以数组的长度:
for ( int i = 0; i < hoursArray.Length; ++i )
{
// get the lowest value
parkFee = Math.Min(hoursArray[i] * HOURLY_RATE, MAX_FEE);
// add to total fees
total += parkFee;
// print current iteration state
Console.WriteLine("Hours : {0}, Parking fee : {1}", hoursArray[i], parkFee);
}
// calculate the average
average = total / (double)hoursArray.Length;
// print results
Console.WriteLine("Average : {0}", average.ToString("N2"));
https://stackoverflow.com/questions/43250025
复制相似问题