c#从for循环中的数组索引中减去一个数字

时间:2017-03-21 08:47:13

标签: c# arrays

我需要从平均值中找出数组中每个数值的差异。我需要循环遍历每个值并从平均值中减去每个值并显示差异。我已经尝试了几种不同的方法,但差异总是在结尾时为0。我在这做错了什么?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;

namespace AvgNDiff
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] numbers = new int[10];
            int x = 0;
            int i;
            string entryString = "";
            int counter = 0;
            int countdown = 10;
            int sum = 0;
            int average = 0;


            while (counter < numbers.Length && numbers[x] != 10 && entryString != "0")
            {
                if (x == 0)
                    Write("Enter up to 10 numbers or type 0 to stop > ");
                else if (x == 9)
                    Write("Enter {0} more number or type 0 to stop > ", countdown);
                else
                    Write("Enter up to {0} more numbers or type 0 to stop > ", countdown);

                entryString = ReadLine();
                numbers[x] = Convert.ToInt32(entryString);


                if (entryString != "0")
                {
                    sum += numbers[x];
                    counter++;
                    x++;
                }

                countdown--;
            }

            average = sum / x;

            WriteLine("\n\nYou entered {0} numbers with a sum of {1}", x, sum);
            WriteLine("The average of your numbers is " + average);

            WriteLine("\n\nNumber       Difference");
            WriteLine("-------------------------------");



            for (i=0; i < x; i++)
            {
                int value = numbers[i];
                int diff = average-value;

                WriteLine(String.Format("{0,-10}   {1,-10}", (numbers[i]), diff));
            }


            ReadKey();




        }
    }
}

3 个答案:

答案 0 :(得分:1)

看看这里

int value = numbers[i];
int diff = value - average;
WriteLine(String.Format("{0,-10}   {1,-10}", (numbers[i]), value));

这里的关键问题是令牌声明。

你已经告诉它显示numbers[i],哦等等.. numbers[i](因为那是什么价值)

但差异包含与平均值的差异......

答案 1 :(得分:0)

static void Main(string[] args)
{
    List<int> numberList = new List<int>();

    Console.WriteLine("Enter up to 10 numbers or type 0 to stop:");

    for (int i = 0; i < 10; i++)
    {
        int userInput = 0;

        while (!int.TryParse(Console.ReadLine(), out userInput))
        {
            Console.WriteLine("Only integer numbers accepted, let's try again...:");
        }

        if (userInput == 0)
        {
            break;
        }
        else
        {
            numberList.Add(userInput);

            if (i < 9)
            {
                Console.WriteLine("Yeah, {0} more number{1} to go!", (9 - i), (i == 8 ? "" : "s"));
            }
        }
    }

    double average = numberList.Average();

    for (int i = 0; i < numberList.Count; i++)
    {
        Console.WriteLine("#{0}: {1} - {2} = {3}", (i+1), numberList[i], average, (numberList[i] - average));
    }

    Console.ReadLine();
}

答案 2 :(得分:0)

不要将整个灵魂集成到单Main中,提取(并调试)方法:

   // Subtract each item from the average
   private static double[] MyNormalize(int[] source) {
     double sum = 0.0;

     foreach (var item in source)
       sum += item;

     double[] result = new double[source.Length];

     for (int i = 0; i < source.Length; ++i) 
       result[i] = sum / source.Length - source[i];

     return result;
   }

   ... 

   static void Main(string[] args) {
     int[] numbers = new int[10]; 

     ...
     // Input
     while (counter < numbers.Length && numbers[x] != 10 && entryString != "0") {
       ... 
       entryString = ReadLine();
       numbers[x] = Convert.ToInt32(entryString);
     }

     // Business logic
     double[] norm = MyNormalize(numbers);

     // Output
     for (int i = 0; i < numbers.Length; ++i)
       WriteLine(String.Format("{0,-10}   {1,-10}", numbers[i], norm[i]));  
   }