For entering the loop through the bad (lower)

Exercise 1: Find all even and 1 to 100?

            int sum = 0;//声明一个变量存储总和
            for (int i = 1; i <= 100; i++)
            {
                if (i % 2 == 0)//偶数整除2余数等于0
                {
                    sum += i;//把符合条件的值累加到sum
                }
            }
            Console.WriteLine(sum);//输出sum
            Console.ReadKey();

Question 2: Find the number between 100-999 daffodils?

            for (int i = 100; i <= 999; i++)
            {
                int hun = i / 100;//除以100得到百位数字
                int ten = i % 100 / 10;//对100取余得到的余数再除以10得到十位上的数字
                int n = i % 10;//直接对10取余得到个位上的数字

                if (hun * hun * hun + ten * ten * ten + n * n * n == i)//如果循环的这个数字等于i这个数就是水花仙数
                {
                    Console.WriteLine("100到999之间的水仙花数有{0}", i);
                }
            }
            Console.ReadKey();

Exercise 3: 5 cycles Input individual's age and the average age, if entered data appear negative or greater than the number 100, stop immediately and report an error input

            int sum = 0;//声明变量存储总和
            for (int i = 1; i <= 5; i++)
            {
                Console.WriteLine("请输入第{0}个人的年龄", i);//提示用户输入年龄
                int age = int.Parse(Console.ReadLine());//接收用户输入

                if (age < 0 || age > 100)//如果输入的年龄超出题目范围
                {
                    Console.WriteLine("输入错误,程序退出");
                    break;//输入错误跳出循坏体
                }
                else//符合条件
                {
                    sum += age;
                }
                if (i >= 5)//输入年龄的次数大于5次
                {
                    Console.WriteLine("5人的平均年龄为{0}", sum / 5);//计算5人的平均年龄
                }
            }
            Console.ReadKey();

Exercise four: an integer ranging from 1 to 100 is added to obtain a current integrated value is greater than 20
(for example: 1 + 2 + 3 + 4 + 5 + 6 = 21) Results 6 sum> = 20

            int sum = 0;//存储总和
            for (int i = 1; i <= 100; i++)
            {
                sum += i;
                if (sum >= 20)//总和大于等于20
                {
                    Console.WriteLine("加到{0}的时候,总和大于了20",i);
                    break;//条件成立跳出循坏
                }
            }
            Console.ReadKey();

for loop
Syntax:
for (Expression 1; 2 Expression; Expression 3)
{
loop body;
}
expression statement is a loop variable frequency and generally, the recording cycle (int i = 0;)
expression cycling conditions typically 2 (i <10)
expression 3 is generally cycling conditions to change the code, one day the circulation condition is no longer true (i ++).
Execution: The program starts executing the expression 1, declare the loop variable used to record the number of times a loop,
and then execute the expression 2, determine the loop condition is satisfied, if the expression evaluates to 2 returns true,
the loop body is executed. After one complete loop, performed expression 3, expression 2 then continues to determine execution cycle condition is established,
if the establishment of the loop continues, if not true, out of for loop.

Published 17 original articles · won praise 0 · Views 735

Guess you like

Origin blog.csdn.net/weixin_44623941/article/details/104871712