-2

这是问题所在:

编写一个名为 TipsList 的程序,它接受七个双精度值,代表服务员在一周中每天赚取的小费。显示每个值以及指示它与平均值相差多远的消息。

这是我到目前为止所知道的。

   static void Main(string[] args)
    {
        double[] tips;
        tips = new double[7];

        double one = tips[0];
        double two = tips[1];
        double three = tips[2];
        double four = tips[3];
        double five = tips[4];
        double six = tips[5];
        double seven = tips[6];

        double average = (one + two + three + four + five + six + seven) / 7;

        //Now I am trying to take the tip 1,2,3,4,5,6, and 7 that the user has entered
        //And display the diffrence of tip 1,2,3,4,5,6, and 7 from the average
        //So one-average = tip 1 console.Write tip1 ??????

        for (int i = 0; i <= 6; i++)
        {

            Console.Write("Please enter the amount of tips earned by waiter #" + i + ".");

            tips[i] = Console.Read();

            Console.Write("tips 1????????????HELP");

        }

    }
}

我了解我将如何尝试并认为我应该这样做

one-average = tip 1 console.Write tip1???

但 C# 不喜欢它。我只是不明白它仍然 C# 只让我以一种确定的方式去做。

4

3 回答 3

2

我刚刚意识到这是为了上课,所以我会远离 Linq,这对任何老师来说都太明显了。

只需写出从平均值中取出的每个值

foreach(double tip in tips)
{
Console.WriteLine(Average - tip);
}

编辑刚刚意识到问题是获取输入。

你最好使用TryParse,因为这将处理无效输入

while(!double.TryParse(Console.ReadLine(), out tips[i]))
{
    Console.WriteLine("Thats not a valid number");
}
于 2013-09-17T07:38:46.357 回答
0

使用这样的东西:

double[] tips = new double[7];

for (int i = 0; i < tips.Length; i++)
{
    Console.Write("Please enter the amount of tips earned by waiter #" + i + ": ");
    tips[i] = double.Parse(Console.ReadLine());
}

double average = tips.Average();

//without linq
/*
double sum = 0;
for (int i = 0; i < tips.Length; i++)
{
    sum = sum + tips[i];
}
double average = sum / tips.Length;
*/

for (int i = 0; i < tips.Length; i++)
{
    Console.WriteLine("Tip #" + i + " is: " + tips[i] + " - The difference between the average is: " + Math.Abs(tips[i] - average));
}

Console.ReadLine()
于 2013-09-17T07:37:44.743 回答
0

我自己正在做这个程序,我意识到它实际上是在要求一个二维数组,因此一周中的 7 天有 7 个输入。您可以通过使用double[,] tips = new double[7, 7];Then 来完成此操作,您将使用 2 个循环来访问每个索引元素

 for (int i = 0; i < 7; i++)
     {
       for (int j = 0; j < 7; j++)
            {
              tips[i, j] = int.Parse(Console.ReadLine());
            }
      }`

然后你会首先得到一个平均值(即添加每天(7)或一周(49)的所有索引的总和,具体取决于您希望数据的准确性,然后除以)

于 2019-05-11T19:41:26.120 回答