.Net基础篇_学习笔记_第五天_流程控制while循环

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

namespace 第五天_流程控制02
{
    class Program
    {
        static void Main(string[] args)
        {
            int i = 0;
            int s = 0;
            while (i < 100)
            {
                if (i % 2 == 0)
                {
                    s += i;
                    Console.WriteLine("当前累加到的数字为{0},累加的和为{1}",i,s);
                }
                i++;
            }
            Console.ReadKey();
        }
    }
}

0-100所有偶数相加的结果。

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace 第五天_流程控制02
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             int i = 0;
14             int j = 0;
15             int s = 0;
16             while (i <= 10)
17             {
18                 while (j <= 10)
19                 {
20                     Console.WriteLine("当前是while循环中的内循环,j的值为{0}",j);
21                     j++;
22                     break;
23                 }
24                 i++;
25                 Console.WriteLine("当前是while循环中的外循环,i的值为{0}", i);
26             }
27            
28             Console.ReadKey();
29         }
30     }
31 }

break跳出的是当前while循环。

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace 第五天_流程控制02
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             int _scoreAll = 0;
14             Console.WriteLine("请输入班级总人数:");
15             int _classNum = Convert.ToInt32(Console.ReadLine());
16             int i = 1;
17             while (i <= _classNum)
18             {
19                 Console.WriteLine("请输入第{0}个同学的成绩:",i);
20                 int _scoreMan = Convert.ToInt32(Console.ReadLine());
21                 _scoreAll +=_scoreMan;
22                 i++;           
23             }
24             Console.WriteLine("全班共有{0}个同学,总成绩为:{1},平均成绩为:{2}",_classNum,_scoreAll,_scoreAll/_classNum);
25             Console.ReadKey();
26         }
27     }
28 }

while循环结构的应用。

while循环最易遗忘的点是i++。

原文地址:https://www.cnblogs.com/NBOWeb/p/7126318.html