C#输入一组非0整数(以0作为输入结束标志)到一维数组中,求出这一组数的平均值,统计出正数和负数的个数.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace baidu_shurutongji
{
class Program
{
static void Main(string[] args)
{
int a = 1, num1 = 0, num2 = 0;
float average = 0; //a存储输入的整数,num1表示正数个数,num2表示负数个数,average表示平均数
while (a != 0)
{
System.Console.Write("请输入一个整数(输入0表示输入结束):");
a = Int32.Parse(System.Console.ReadLine());
if (a > 0)
{
num1++;
average += a;
}
else if (a < 0)
{
num2++;
average += a;
}
else
{
average = average / (num1 + num2);
}
}
System.Console.WriteLine("平均数为:"+average+"\t正数个数:"+num1+"\t负数个数:"+num2+"\n");
Console.ReadLine();
}
}
}
int n;
int sum = 0;
int[] count=new int[1000];
int pos = 0;
int neg=0;
Console.WriteLine("请输入(以0作为输入结束标志)");
n = int.Parse(Console .ReadLine ());
for (int i = 0; n != 0;i++ )
{
count[i] = n;
sum = sum + n;
if (n > 0) pos++;