如何为以下情况创建循环?

时间:2011-06-30 19:32:38

标签: c# loops

/html/body/table/tr[1]/td[2]
/html/body/table/tr[1]/td[4]

/html/body/table/tr[3]/td[2]
/html/body/table/tr[3]/td[4]

/html/body/table/tr[5]/td[2]
/html/body/table/tr[5]/td[4]

因此,tr []的索引是奇数,td []总是2或4。

3 个答案:

答案 0 :(得分:4)

for(int i = 1; i < bound; i += 2) {
    for(int j = 2; j <= 4; j += 2) {
        Console.WriteLine(
            String.Format("/html/body/table/tr[{0}]/td[{1}]", i, j)
        );
    }
    Console.WriteLine();
}

答案 1 :(得分:3)

你可以做一些简单的事情

for(tr = 1; tr < maxodd+1; tr += 2;)
{
    //pseudoimplementation
    /html/bod/table/tr[tr]/td[2]
    /html/bod/table/tr[tr]/td[4]

}

答案 2 :(得分:2)

最天真的案例:

for(int i = 1; i < 6; i += 2) {
    Console.WriteLine("html/body/table/tr[" + i + "]/td[2]");
    Console.WriteLine("html/body/table/tr[" + i + "]/td[4]");
}