c#do while语句实现1到100求和?

给出详细代码
2024-11-27 03:46:32
推荐回答(3个)
回答1:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            int i = 1;
            int sum=0;
            do 
            {
               sum=i+sum;
               i++;
            } while (i<=100);
        }
    }
}

回答2:

int num=1;
int sum=0;
do
{
sum += num;
num++;
} while (num < 101);
Console.WriteLine(sum);
Console.Read();

回答3:

class Program
{
static void Main(string[] args)
{
int sum = 1;
int i = 2;
do
{

sum += i;
i++;
}
while(i<101);
Console.WriteLine(sum);
Console.Read();

}
}