C#数组平均数问题

yixibing 2013-04-11 08:45:46
作为一名菜鸟,写完这个程序后,发觉竟然求不了平均数,老是为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();
}
}
}
...全文
199 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
yixibing 2013-04-11
  • 打赏
  • 举报
回复
谢谢楼上两位,受教了!
熊猫无敌 2013-04-11
  • 打赏
  • 举报
回复
同上,你的int x是局部变量 还有,程序不要用goto 用while或其它代替goto
  • 打赏
  • 举报
回复
int x = Convert.ToInt32(s); => arr[i]= Convert.ToInt32(s);

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧