我想读取小数值并使用它做出决定。如果值大于0.75则存储一个案例否则不存在。我正在划分4/5,答案是0.8,但变量是存储0而不是0.8。
班级代码如下:
float value;
List<crime> finalList = new List<crime>();
Dictionary<int,float> finalDictionary = new Dictionary<int,float>();
public float matching()
{
string crimeType = null;
string keys = null;
List<crime> keywordsFromOldCrimes = new List<crime>();
List<String> keywordsFromCrimes = new List<string>();
List<string> commonWords = new List<string>();
SqlConnection conn = new SqlConnection(@"Data Source=DELL\SQLEXPRESS;initial catalog=CDMIS;integrated security=True;MultipleActiveResultSets=true;");
SqlCommand commd = new SqlCommand("select * from crime where id=0 ", conn);
SqlCommand commd1 = new SqlCommand("select * from crime", conn);
SqlCommand commd2 = new SqlCommand("select crime.id , crime.crimeType, crime.keywords from crime inner join FIR on crime.FIRid= FIR.id where FIR.incidentDate in (select from FIR where datepart(yy, incidentDate)>2015)", conn);
SqlCommand com3 = new SqlCommand("select * from crime where crimedate >= '2015-01-01'",conn);
SqlDataReader read1;
SqlDataReader read2;
try
{
conn.Open();
read1 = commd.ExecuteReader();
while (read1.Read())
{
crimeType = read1.GetString(read1.GetOrdinal("crimeType"));
keys = read1.GetString(read1.GetOrdinal("keywords"));
}
}
catch (Exception exp)
{
throw exp;
}
read1.Close();
List<string> keywords = keys.Split(',').ToList();
// read2 = commd2.ExecuteReader();
//while (read2.Read())
//{
// keywords.Add(new crime()
// {
// id = read2.GetInt32(read2.GetOrdinal("id")),
// keywords = read2.GetString(read2.GetOrdinal("keywords")),
// crimeType = read2.GetString(read2.GetOrdinal("crimeType"))
// });
//}
//read2.Close();
//foreach (var word in keyword)
//{
// if (word.keywords == keys)
// {
// }
//}
read1 = com3.ExecuteReader();
while (read1.Read())
{
keywordsFromOldCrimes.Add(new crime()
{
id = read1.GetInt32(read1.GetOrdinal("id")),
caseStatus = read1.GetString(read1.GetOrdinal("caseStatus")),
crimeType = read1.GetString(read1.GetOrdinal("crimeType")),
details = read1.GetString(read1.GetOrdinal("details")),
keywords = read1.GetString(read1.GetOrdinal("keywords")),
});
}
foreach (var crime in keywordsFromOldCrimes)
{
keywordsFromCrimes = crime.keywords.Split(',').ToList();
commonWords = keywords.Intersect(keywordsFromCrimes).ToList();
value = commonWords.Count() / keywords.Count();
Console.Write(value);
if (value > 0.75)
{
finalDictionary.Add(crime.id, value);
}
else
{
Console.Write("nothing common");
}
}
return (value);
}
}
问题在于以下部分:
foreach (var crime in keywordsFromOldCrimes)
{
keywordsFromCrimes = crime.keywords.Split(',').ToList();
commonWords = keywords.Intersect(keywordsFromCrimes).ToList();
value = commonWords.Count() / keywords.Count();
Console.Write(value);
if (value > 0.75)
{
finalDictionary.Add(crime.id, value);
}
else
{
Console.Write("nothing common");
}
}
根据我的数据库中的数据,commonWords.count()= 4的值和keywords.count()= 5的值,因此变量“value”中的答案应为“0.8”,但它为“0”。
答案 0 :(得分:1)
这部分
commonWords.Count() / keywords.Count()
是整数除法,需要将其转换为浮动才能使其正常工作
commonWords.Count() / (float) keywords.Count()