for循环不起作用

本文关键字:不起作用 循环 for | 更新日期: 2023-09-27 17:54:10

下面的代码只显示The ID Does Not Exist作为结果。有什么问题吗?

int sc = Convert.ToInt32(txtbx_id.Text);
int n = Convert.ToInt32(txtbx_noofrecomm.Text);
bool shart = false;
for (int yu = 0; yu <= 92161; yu++)
{
    int wer = d[yu];
    if (wer == sc)
    {
        shart = true;
        userseq = yu;
        var results = new List<float>(1143600);
        for (int z = 0; z < 1143600; z++)
        {
            results.Add(dotproduct(userseq, z));
        }
        var sb1 = new StringBuilder();
        foreach (var resultwithindex in results.Select((r, index) => new { result = r, Index = index }).OrderByDescending(r => r.result).Take(n))
        {
            sb1.AppendFormat(CultureInfo.InvariantCulture, "{0}: {1}", c[resultwithindex.Index], resultwithindex.result);
            sb1.AppendLine();
        }
        MessageBox.Show(sb1.ToString());
    }
}
if (shart == false)
{
    MessageBox.Show("The ID Does Not Exist");
}

for循环不起作用

d[]不等于sc

尝试删除第二个if语句,然后将第一个if语句变为if - else语句。因此,您可以在将来有效地测试您的数据。您可以将shart设置为false,然后显示消息框。

    if (wer == sc)
{
    shart = true;
    userseq = yu;
    var results = new List<float>(1143600);
    for (int z = 0; z < 1143600; z++)
      {
        results.Add(dotproduct(userseq, z));
      }
    var sb1 = new StringBuilder();
    foreach (var resultwithindex in results.Select((r, index) => new { result = r, Index = index }).OrderByDescending(r => r.result).Take(n))
      {
        sb1.AppendFormat(CultureInfo.InvariantCulture, "{0}: {1}", c[resultwithindex.Index], resultwithindex.result);
        sb1.AppendLine();
      }
    MessageBox.Show(sb1.ToString());
}else{
       shart = false;
       MessageBox.Show("The ID Does Not Exist");
}