日期:2014-05-17  浏览次数:21056 次

C#数组平均数问题
作为一名菜鸟,写完这个程序后,发觉竟然求不了平均数,老是为0;不知所以然了。反了老大劲了,求指教啊。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _1._1
{
    class Program
    {
        static void Main(string[] args)
        {
            int []arr=new int[10];
            for (int i = 0; i < arr.Length; )
            {
            x:  Console.Write("请输入数组arr的第{0}个元素:", i);

                string s = Console.ReadLine();
                if (s == "")
                {
                    Console.WriteLine("不可输入空串,请重新输入!");
                    goto x;
                }
                try
                {
                    int x = Convert.ToInt32(s);
                    i++;
                }
                catch
                {
                    Console.WriteLine("你输入的是非法字符,请重新输入");
                }
               

            }
            
             Console.WriteLine("数组arry的平均值为:{0}", arr.Average());

            Console.ReadLine();
        }
    }
}
C# 数组

------解决方案--------------------
 int x = Convert.ToInt32(s);
=>
arr[i]= Convert.ToInt32(s);

------解决方案--------------------
同上,你的int x是局部变量
还有,程序不要用goto
用while或其它代替goto