就这个要求我不会了:
使用while循环语句,依次读取输入的数值,计算各数之和保存在变量total中,并在变量count中记录输入的个数,当用户输入x或X时跳出循环
--------------------------------------------------------------------------------------------------------------------
希望大家帮忙,感激不尽!
------------------------------------------//这个是我写的小程序,但是没有上面的那个(当用户输入x或X时跳出循环)功能、
static void Main(string[] args)
{
Int32 count = 0;
float total = 0;
string s_input;
bool condition = true; //这个是我临时测试自己程序是否错误的哨兵 Console.WriteLine("Please Put Into Swatch Every Time !\n");
Console.WriteLine("total == {0}", total.ToString());
Console.WriteLine("count == {0}", count.ToString());
Console.WriteLine("-------------------------------------\n");
while (condition)
{
if (count == 4) //这个是我临时测试自己程序是否错误的哨兵
condition = false;
else
{
s_input = Console.ReadLine();
total += Convert.ToInt32(s_input);
count++;
Console.WriteLine("total == {0}", total.ToString());
Console.WriteLine("count == {0}", count.ToString());
}
}
Console.WriteLine("The Number Is {0}\n", (total / count));
}
使用while循环语句,依次读取输入的数值,计算各数之和保存在变量total中,并在变量count中记录输入的个数,当用户输入x或X时跳出循环
--------------------------------------------------------------------------------------------------------------------
希望大家帮忙,感激不尽!
------------------------------------------//这个是我写的小程序,但是没有上面的那个(当用户输入x或X时跳出循环)功能、
static void Main(string[] args)
{
Int32 count = 0;
float total = 0;
string s_input;
bool condition = true; //这个是我临时测试自己程序是否错误的哨兵 Console.WriteLine("Please Put Into Swatch Every Time !\n");
Console.WriteLine("total == {0}", total.ToString());
Console.WriteLine("count == {0}", count.ToString());
Console.WriteLine("-------------------------------------\n");
while (condition)
{
if (count == 4) //这个是我临时测试自己程序是否错误的哨兵
condition = false;
else
{
s_input = Console.ReadLine();
total += Convert.ToInt32(s_input);
count++;
Console.WriteLine("total == {0}", total.ToString());
Console.WriteLine("count == {0}", count.ToString());
}
}
Console.WriteLine("The Number Is {0}\n", (total / count));
}
解决方案 »
- c# 窗体问题
- C#的Hook实例...大家帮帮修改一下我所要的效果,功能...因为我对Hook不了解...去MSDN查了也不了解怎么用...
- 这样的Respon.Redirect为什么不行呢?
- C# 未将对象引用设置到对象的实例
- 求正则表达式
- 在c#里边如何实现安装程序中的“上一步”,“下一步”?
- 急!!!求助:WebService直接启动错误,WebService中可用类型有何限制?
- 如何用代码反转一个checked项的选中状态?
- datagrid中问题,我想大家都遇到过吧??
- 2001年最后一天,凌晨2:30分,服务器可以证明我还在加班。
- string::indexof搜索某个字符串如何不区分大小写?
- 为什么我写的东西读取数据的时候会那么慢阿?
if (s_input=="x" || s_input == "X")
break;
total += Convert.ToInt32(s_input); 或者用这个
if (s_input.Equals("x") || s_input.Equals("X"))
break;
//Int32 count = 0;
//改成int别名...统一风格...
int count = 0;
float total = 0;
string s_input;
bool condition = true;Console.WriteLine("Please Put Into Swatch Every Time:\n");
//Console.WriteLine("total == {0}", total.ToString());
//Console.WriteLine("count == {0}", count.ToString());
//上面两句有用吗?人人都知道是0
//Console.WriteLine("-------------------------------------\n");while (condition)
{
s_input = Console.ReadLine();
//total += Convert.ToInt32(s_input);
//ToInt32只保留整数...改用TryParse避免异常...
float tmp = 0;
if (float.TryParse(s_input, out tmp))
{
total += tmp;
count++;
Console.WriteLine("total == {0}", total.ToString());
Console.WriteLine("count == {0}", count.ToString());
}
else
{
//退出循环条件
condition = (s_input.Trim().ToLower() != "x");
}
}
Console.WriteLine("The Number Is {0}\n", (total / count));
{
total += Convert.ToInt32(s_input);
count++;
}
catch
{
Console.WriteLine("Numbers only.");
}