嵌套for循环求和问题
//打印出10行5列的表for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 5; j++)
{
Response.Write("5");
}
Response.Write("<br>");
}
类似上面代码动态生成一张表,怎么求每一列的和,这个算法有点麻烦诶
可以在for循环中算出来吗 --------------------编程问答-------------------- 顶 --------------------编程问答-------------------- 定义五个变量在循环外面,第二层循环里switch(j){ case(0): 第一列的和+=j;break; case(1):第二列的和+=j;break; 大概这样的思路吧 --------------------编程问答--------------------
--------------------编程问答--------------------
int num=0;
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 5; j++)
{
if(j==2) num+=j;
Response.Write("5");
}
Response.Write(" <br>");
}
Response.Write(num.ToString());
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 5; j++)
{
Response.Write("5");
}
Response.Write(" <br>");
if(i==10)
{
for (int k = 0; k < 5; k++)
{
Response.Write(i*j);
}
}
}
是这样吗? --------------------编程问答-------------------- 语法错误自动无视。。。 --------------------编程问答-------------------- // 存储每一列的总和 索引0代表第一列
int[] sum = new int[5];
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 5; j++)
{
Response.Write("5");
sum[j] += 5;
}
Response.Write(" <br>");
} --------------------编程问答-------------------- 为什么不放在datatable里呢 --------------------编程问答--------------------
--------------------编程问答--------------------
for (int i = 0; i < 10; i++)
{
int max = 0;
for (int j = 0; j < 5; j++)
{
Response.Write("5");
max+=5;
}
Response.Write("-----该列和:");
Response.Write(max);
Response.Write(" <br>");
}
int[] count = new int[5];
Response.Write("<table border ='1'>");
for (int i = 0; i < 5; i++)
{
Response.Write("<tr>");
for (int j = 0; j < 5; j++)
{
int k = i + j;
Response.Write("<td>"+k+"</td>");
count[i] += k;
}
Response.Write("</tr>");
}
Response.Write("<tr>");
for (int m = 0; m < count.Length; m++)
{
Response.Write("<td>"+count[m]+"</td>");
}
Response.Write("</tr>");
Response.Write("</table>");
补充:.NET技术 , ASP.NET