for (int k = 0; k < 32;k++)
{
for (int j = 0; j < 32; j++)
{
double Score = (user0[k] * user0[j] + user1[k] * user1[j] + user2[k] * user2[j] + user3[k] * user3[j] + user4[k] * user4[j] + user5[k] * user5[j] + user6[k] * user6[j] +
user7[k] * user7[j] + user8[k] * user8[j] + user9[k] * user9[j] + user10[k] * user10[j] + user11[k] * user11[j] + user12[k] * user12[j] + user13[k] * user13[j] + user14[k] * user14[j] + user15[k] * user15[j] + user16[k] * user16[j] + user17[k] * user17[j] +
user18[k] * user18[j] + user19[k] * user19[j] + user20[k] * user20[j] + user21[k] * user21[j] + user22[k] * user22[j] + user23[k] * user23[j] +
user24[k] * user24[j] + user25[k] * user25[j] + user26[k] * user26[j] + user27[k] * user27[j] + user28[k] * user28[j] + user29[k] * user29[j] +
user30[k] * user30[j] + user31[k] * user31[j]) / ((Math.Sqrt(user0[k] * user0[k] + user1[k] * user1[k] + user2[k] * user2[k] + user3[k] * user3[k] + user4[k] * user4[k] + user5[k] * user5[k] + user6[k] * user6[k] + user7[k] * user7[k] + user8[k] * user8[k] + user9[k] * user9[k] +
user10[k] * user10[k] + user11[k] * user11[k] + user12[k] * user12[k] + user13[k] * user13[k] + user14[k] * user14[k] + user15[k] * user15[k] + user16[k] * user16[k] + user17[k] * user17[k] +
user18[k] * user18[k] + user19[k] * user19[k] + user20[k] * user20[k] + user21[k] * user21[k] + user22[k] * user22[k] + user23[k] * user23[k] + user24[k] * user24[k] +
user25[k] * user25[k] + user26[k] * user26[k] + user27[k] * user27[k] + user28[k] * user28[k] + user29[k] * user29[k] + user30[k] * user30[k] + user31[k] * user31[k])) * (Math.Sqrt(user0[j] * user0[j] + user1[j] * user1[j] + user2[j] * user2[j] + user3[j] * user3[j] +
user4[j] * user4[j] + user5[j] * user5[j] + user6[j] * user6[j] + user7[j] * user7[j] + user8[j] * user8[j] + user9[j] * user9[j] + user10[j] * user10[j] + user11[j] * user11[j] +
user12[j] * user12[j] + user13[j] * user13[j] + user14[j] * user14[j] + user15[j] * user15[j] + user16[j] * user16[j] + user17[j] * user17[j] +
user18[j] * user18[j] + user19[j] * user19[j] + user20[j] * user20[j] + user21[j] * user21[j] + user22[j] * user22[j] + user23[j] * user23[j] + user24[j] * user24[j]
+ user25[j] * user25[j] + user26[j] * user26[j] + user27[j] * user27[j] + user28[j] * user28[j] + user29[j] * user29[j] + user30[j] * user30[j] + user31[j] * user31[j])));
if (Score > simScore)
{
simScore = Score;
}
}
如果我想在k = j时不计算循环,我该怎么做呢? 当k = j时计算循环时我的代码计算乱了,因为它将取32个结果中的最大值,如果计算k = j循环,则最大值将始终是j = k的情况。
答案 0 :(得分:9)
在你的循环中使用continue
:
for (int k = 0; k < 32; k++)
{
for (int j = 0; j < 32; j++)
{
if (k == j)
continue;
// ...
}
}
答案 1 :(得分:1)
不确定这是否是强迫症,但你强迫我告诉你:
double[,] user = new double[32,32];
for (int k = 0; k < 32; k++)
{
for (int j = 0; j < 32; j++)
{
if (k == j)
continue;
double Score = 0.0;
for (int i = 0; i < 32; i++)
{
Score = (user[i,k] * user[i,j]) / Math.Sqrt(user[i,k] * user[i,k]) * Math.Sqrt(user[i,j] * user[i,j] );
if (Score > simScore)
{
simScore = Score;
}
}
}
}
答案 2 :(得分:0)
使用Continue:
Continue改变控制流程。它经常出现在循环体中。它允许您跳过迭代的其余部分的执行。它会立即跳转到循环中的下一个迭代。此关键字在while循环中通常很有用。
while (true)
{
// Get a random number.
int value = random.Next();
// If number is divisible by two, skip the rest of the iteration.
if ((value % 2) == 0)
{
continue;
}
// If number is divisible by three, skip the rest of the iteration.
if ((value % 3) == 0)
{
continue;
}
Console.WriteLine("Not divisible by 2 or 3: {0}", value);
// Pause.
Thread.Sleep(100);
}