class Program
{
static void Main(string[] args)
{
float y = 0, n = 0;
int x = Convert.ToInt32(Console.ReadLine());
while (x != -1)
{
n++;
y += x;
x = Convert.ToInt32(Console.ReadLine());
}
if (n == 0)
Console.WriteLine(y);
else
Console.WriteLine(y/n);
Console.ReadLine();
}
}
{
static void Main(string[] args)
{
float y = 0, n = 0;
int x = Convert.ToInt32(Console.ReadLine());
while (x != -1)
{
n++;
y += x;
x = Convert.ToInt32(Console.ReadLine());
}
if (n == 0)
Console.WriteLine(y);
else
Console.WriteLine(y/n);
Console.ReadLine();
}
}
{
static void Main(string[] args)
{
float y = 0, n = 0;
int x = Convert.ToInt32(Console.ReadLine()); 把读取的该行的数据转换成int
while (x != -1)
{
n++;
y += x;
x = Convert.ToInt32(Console.ReadLine());
}
if (n == 0)
Console.WriteLine(y);
else
Console.WriteLine(y/n);
Console.ReadLine();
}
}
{
n++;
y += x;
x = Convert.ToInt32(Console.ReadLine());
}
这个不会出现死循环的吗?
在while循环的外面,进去就出不来了,哪还有输入-1的机会?
是不是用try...catch一下?
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace 求n个数的平均数
{
class Program
{
static void Main(string[] args)
{
float n = 0, sum = 0,x;
try
{
x = Convert.ToInt32(Console.ReadLine());
}
catch
{
Console.WriteLine("输入格式错误!");
Console.ReadKey();
//输入错误后,用goto语句怎么跳回前面重新输入?
}
while (x != -1)
{
n++;
sum = sum + x;
x = Convert.ToInt32(Console.ReadLine());
}
float averager;
averager = sum / n;
Console.WriteLine(averager);
Console.ReadKey();
}
}
}
如上,//输入错误后,用goto语句怎么跳回前面重新输入?