怎么获得List<int>里面数字当前的趋势? 遍历list,做如下操作即可1. 第二个和第一个对比如果是上升这记录上升,并将结果保存到一个临时变量2.第三个和第二个对比如果对比结果和临时变量不一致则记录当前结果,吧结果保存到临时变量,如果对比结果一致则不做处理。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 for int i=0 i<list.count i++{var x=list[i];cw(i==0?"起":(x>list[i-1]?"上升":"下降"));} List<int> list = new List<int>(); list.Add(15); list.Add(12); list.Add(16); list.Add(14); list.Add(13); list.Add(10); list.Add(18); for (int i = 0; i < list.Count; i++) { var x = list[i]; Response.Write(i == 0 ? "起<br>" : (x > list[i - 1] ? "上升" : "下降")+"<br>"); }得到起下降上升下降下降下降上升应该和你胃口吧.. 可能我表达的不够清楚,像我上面给出的数字组,其实就是4段趋势,就是 上升->下降->上升->下降 . 例如数字 2 ,5,10,其实他们都是上升的,只需出现一次上升就行了.怎么写才能达到这样的效果呢? 把“上升”“下降”放到一个新的list中,遍历,如果和前一个一样就remove掉,最终的list应该就是你想要的了 List<double> list = new List<double> { 39.5, 41.5, 43.5, 50, 48, 48.5, 49, 49.5, 48.5, 47.5, 46.5 }; int trend = 0; for (int i = 1; i < list.Count; i++) { if (trend == 0) { if (list[i] > list[i - 1]) { trend = 1; Console.WriteLine("上升"); } else { trend = -1; Console.WriteLine("下降"); } } else if (trend == 1 && list[i] < list[i - 1]) { Console.WriteLine("下降"); trend = -1; } else if (trend == -1 && list[i] > list[i - 1]) { Console.WriteLine("上升"); trend = 1; } } List<int> list = new List<int>(); list.Add(15); list.Add(12); list.Add(16); list.Add(14); list.Add(13); list.Add(10); list.Add(18); List <string> result=new List<string> (); for(int i=0;i<list.Count;i++) { if (i == 0) result.Add("Start"); else if (list[i] > list[i - 1] && result.Last() != "Up") result.Add("Up"); else if (list[i] < list[i - 1] && result.Last() != "Down") result.Add("Down"); } 求教:DataTable多层表头及合并 如何提交创建数据库的语句给SQL去执行? 主表的主键为自动增长字段所带来问题 这个C++ 函数怎么调用? 属性网格显示复合对象的问题 ListView图标大小 请大家帮忙,把 string 分割成 string[] 的问题。 最后100分,请教一些关于Winform中关于DataGrid的两个简单问题 问一个关于Encoding的问题 @@@@@@@@@@c#里面汉字字符串默认的编码是什么编码? 怎么把TextBox(多行模式)的内容直接赋值给 List<string>? 一个DataTable引用另一个Table会占用双倍资源吗
for int i=0 i<list.count i++
{
var x=list[i];
cw(i==0?"起":(x>list[i-1]?"上升":"下降"));
}
List<int> list = new List<int>();
list.Add(15);
list.Add(12);
list.Add(16);
list.Add(14);
list.Add(13);
list.Add(10);
list.Add(18);
for (int i = 0; i < list.Count; i++)
{
var x = list[i];
Response.Write(i == 0 ? "起<br>" : (x > list[i - 1] ? "上升" : "下降")+"<br>");
}
得到
起
下降
上升
下降
下降
下降
上升
应该和你胃口吧..
List<double> list = new List<double> { 39.5, 41.5, 43.5, 50, 48, 48.5, 49, 49.5, 48.5, 47.5, 46.5 };
int trend = 0;
for (int i = 1; i < list.Count; i++)
{
if (trend == 0)
{
if (list[i] > list[i - 1])
{
trend = 1;
Console.WriteLine("上升");
}
else
{
trend = -1;
Console.WriteLine("下降");
}
}
else if (trend == 1 && list[i] < list[i - 1])
{
Console.WriteLine("下降");
trend = -1;
}
else if (trend == -1 && list[i] > list[i - 1])
{
Console.WriteLine("上升");
trend = 1;
}
}
List<int> list = new List<int>();
list.Add(15);
list.Add(12);
list.Add(16);
list.Add(14);
list.Add(13);
list.Add(10);
list.Add(18); List <string> result=new List<string> ();
for(int i=0;i<list.Count;i++)
{
if (i == 0)
result.Add("Start");
else if (list[i] > list[i - 1] && result.Last() != "Up")
result.Add("Up");
else if (list[i] < list[i - 1] && result.Last() != "Down")
result.Add("Down");
}