c#用for语句怎么计算1到100的和

2024-12-03 16:44:44
推荐回答(5个)
回答(1):

楼上代码是对的,我复制的给你改了一下,下面是整段代码,满意的话把分给楼上的
protected void Page_Load(object sender, EventArgs e)
{
//输出查看
Response.Write(Sum(100));
}
///


/// 求1-n的和
///

///
///
public int Sum(int n)
{
int Sum = 0;
for (int i = 1; i <= n; i++)
{
Sum += i;
}
return Sum;
}

回答(2):

int aa=0;
for (int i = 1; i <= 100; i++)
{
aa = aa+i;
}
Console.Write(aa);
Console.ReadLine();

回答(3):

protected int Sum()
{
int Sum100 = 0;
for (int i = 1; i <= 100; i++)
{
Sum100 += i;
}
return Sum100;
}

回答(4):

int j=101;
int i=100/2;
int sum;
sum=i*j搞定

回答(5):

......楼上是c#语言,你直接调用Sum方法就成了