就这个要求我不会了:
使用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#在 windowform中的textbox
- 求局域网最快速判断webservice是否可用的方法!
- 怎样做到文字浮动?
- 求DataGridView在离开行之前的事件
- C# 操作xls 怎样在程序中调用 xls 里的公式进行计算,计算完后进行数据插入xls
- c# ListView缩略图显示问题
- 程序写好了,想做界面,都有哪些库可以用
- 程序运行后,如何用鼠标托动控件??(WinForm中)
- 请教专家,怎样实现一个自己的Calendar类
- 请问用C#写word的时候怎么设置行间距为固定值28磅
- 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.");
}