请问如何在C#中使用while循环打印出满足下类条件的值:

处于100到10000之间,并且各位数之和等于7(如241:2+4+1=7)
2024-11-25 13:57:44
推荐回答(2个)
回答1:

int i = 100;
while(i<=7000)
{
    int sum = 0,j=0;
    string str=i.ToString();
    while(j    {
        sum += str[j] - 48;
        if (sum > 7) break;
        j++;
    }
    if (sum == 7) Console.Write(i+",");
    i++;
}
Console.ReadKey();

回答2:

static void Main(string[] args)
        {
            int i=100;
            while (i<=10000)
                {
                int s = 0;
                while (i != 0)
                {
                    s += i % 10;
                    i /= 10;
                }
                    if(s==7){
                    Console.WriteLine("{0}", i);}
                }
            Console.ReadKey();
        }