我在应用程序会话期间多次计算小数netscore
,并且我想在每次更改时比较变量,以便识别三个最大值和三个最小值。
我目前正在储存三个最大的&会话状态中的三个最小值。以下代码并不总是正常工作 - 例如,如果我按此顺序将netscore
设置为以下值:57,64,27,56,45,53,62,42,64,40,53,71,57,54,50
它将返回71,71,64
作为三个最大值。我希望看到71,64,64
。
有人可以确定我做错了什么吗?
Session["top1"] = "0";
Session["top2"] = "0";
Session["top3"] = "0";
Session["bottom1"] = "100";
Session["bottom2"] = "100";
Session["bottom3"] = "100";
//Fetch values required to calculate netscore
SqlCommand fivecmd = new SqlCommand(query5, mySLTConnection);
var fives = Convert.ToSingle(fivecmd.ExecuteScalar());
SqlCommand fourcmd = new SqlCommand(query4, mySLTConnection);
var fours = Convert.ToSingle(fourcmd.ExecuteScalar());
SqlCommand threecmd = new SqlCommand(query3, mySLTConnection);
var threes = Convert.ToSingle(fourcmd.ExecuteScalar());
SqlCommand twocmd = new SqlCommand(query2, mySLTConnection);
var twos = Convert.ToSingle(twocmd.ExecuteScalar());
SqlCommand onecmd = new SqlCommand(query1, mySLTConnection);
var ones = Convert.ToSingle(onecmd.ExecuteScalar());
mySLTConnection.Close();
//Get total count
var total = fives + fours + threes + twos + ones;
//Get net score
var netscore = Convert.ToDecimal((((fives + fours) - (twos + ones)) / total)*100);
netscore = Math.Round(netscore,0);
//Begin comparing netscore to stored top/bottom values
if (netscore > Convert.ToDecimal(Session["top1"]))
{
Session["top3"] = Session["top2"];
Session["top2"] = Session["top1"];
Session["top1"] = netscore;
Session["top1q"] = question.ToUpper();
}
else if (netscore > Convert.ToDecimal(Session["top2"]))
{
Session["top3"] = Session["top2"];
Session["top2"] = netscore;
Session["top2q"] = question.ToUpper();
}
else if (netscore > Convert.ToDecimal(Session["top3"]))
{
Session["top3"] = netscore;
Session["top3q"] = question.ToUpper();
}
else if (netscore < Convert.ToDecimal(Session["bottom1"]))
{
Session["bottom3"] = Session["bottom2"];
Session["bottom2"] = Session["bottom1"];
Session["bottom1"] = netscore;
Session["bottom1q"] = question.ToUpper();
}
else if (netscore < Convert.ToDecimal(Session["bottom2"]))
{
Session["bottom3"] = Session["bottom2"];
Session["bottom2"] = netscore;
Session["bottom2q"] = question.ToUpper();
}
else if (netscore < Convert.ToDecimal(Session["bottom3"]))
{
Session["bottom3"] = netscore;
Session["bottom3q"] = question.ToUpper();
}
lblSVal1.Text = Session["top1"].ToString();
lblSVal2.Text = Session["top2"].ToString();
lblSVal3.Text = Session["top3"].ToString();
答案 0 :(得分:1)
快速 - 但不是很优雅 - 修复使用此模式:
select @counter= count(*) from place p join inserted n
where p.address=n.address and p.city=n.city and p.postcode=n.postcode
and p.number=n.number;
答案 1 :(得分:1)
This SO question pretty much answers your question, but I noticed you are also storing some related question data. I first then recommend making a little bit of a class to store the related data together.
static String[] findWord(String x, String[] y){
ArrayList<String> arrList = new ArrayList<String>();
for(String s: y){
if(s.toLowerCase().contains(x.toLowerCase()))
arrList.add(s);
}
String[] arr = arrList.toArray(new String[arrList.size()]);
if(!arrList.isEmpty())
return arr;
else
return new String[0];
}
Then here is a method that would generate a netscore (add arguments so it can build the queries correctly). Also since you didn't detail how the message was loaded, I assume you can figure that out.
class NetScore {
public decimal NetScore;
public string Message;
}
For your main code, you can load them all into a list. Since you didn't detail how you get all the net scores, I just put a foreach for whatever loop you need to do that. This basically will call the method above to generate your message/netscore objects into a list. Once it's in a list its easy to get the top3 and bottom3 with linq.
public NetScore GetNetScore() {
//Fetch values required to calculate netscore
SqlCommand fivecmd = new SqlCommand(query5, mySLTConnection);
var fives = Convert.ToSingle(fivecmd.ExecuteScalar());
SqlCommand fourcmd = new SqlCommand(query4, mySLTConnection);
var fours = Convert.ToSingle(fourcmd.ExecuteScalar());
SqlCommand threecmd = new SqlCommand(query3, mySLTConnection);
var threes = Convert.ToSingle(fourcmd.ExecuteScalar());
SqlCommand twocmd = new SqlCommand(query2, mySLTConnection);
var twos = Convert.ToSingle(twocmd.ExecuteScalar());
SqlCommand onecmd = new SqlCommand(query1, mySLTConnection);
var ones = Convert.ToSingle(onecmd.ExecuteScalar());
mySLTConnection.Close();
//Get total count
var total = fives + fours + threes + twos + ones;
//Get net score
return new NetScore() {
NetScore = Math.Round(Convert.ToDecimal((((fives + fours) - (twos + ones)) / total)*100), 0),
Message = //however you get the message...
}
}
Then you can use the enumerable to write the values out to your labels like this.
List<NetScore> netScores = new List<NetScore>();
// load all the net scores and their messages
foreach(... in ...) {
netScore.Add(GetNetScore());
}
// get the top3 and bottom3
IEnumerable<NetScore> top3 = netScores.OrderByDescending(s => s.NetScore).Take(3);
IEnumerable<NetScore> bottom3 = netScores.OrderBy(s => s.NetScore).Take(3);