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();
}
}
解决方案 »
- 能否提供C#读出和写入XML文件方法。最好是循序渐进的那种。能让全面了解。BAIDU都只提一个方法,没有系统的。
- 求解迷宫最佳路径的算法
- 面试题
- 我下载了一个网站国际化的DEMO,但不知道怎么用
- c# web上怎么制作一个视频播放器的可拖拉的进度条??很急啊,望大狭们指教啊??
- 一个简单的数据库语法问题
- 列举一个动态绑定(又称后绑定)的例子,并说明什么是动态绑定???
- fileshtream中的length属性统计的标准是什么?为何与我的实际不符?
- 排序后在datagrid中的行与dataset的行的对应问题!
- 连接access数据库。。。不设数据库密码连接成功。。如果设了密码系统就提示:
- params参数的用法
- 有没有更好的语音朗读啊
{
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语句怎么跳回前面重新输入?