while循环:循环一次都不执行(先判断在执行)
//实例一(while循环):2006年 80000人,按照25%增长,哪年增长到200000人。
double student = 80000;
int year = 2006;
while (student <= 200000)
{
student *= 1.25;
year++;
}
Console.WriteLine("{0}年是{1}人", year, student);
Console.ReadKey();
do -while 循环:先执行一次循环体,然后判断条件是否成立。
// 要求用户输入用户名和密码,只要不是admin、88888就一直提示用户名或密码错误,请重新输入。
string name = "";
string pwd = "";
do
{
Console.WriteLine("请输入用户名");
name = Console.ReadLine();
Console.WriteLine ("请输入密码");
pwd=Console.ReadLine ();
}while (name!="admin"|| pwd!="88888");
Console.WriteLine ("程序结束");
Console.ReadKey();
//不断要要求用户输入一个数字(假定用户输入的都是正整数),
//当用户输入的q的时候显示刚才输入的数字中的最大值。
string str="";
int number =0;
int max=0;
do
{
Console.WriteLine("请输入正整数");
str = Console.ReadLine();
if (str != "q")
{
number = Convert.ToInt32(str );
if (number > max)
{
max = number;
}
}
} while (str != "q");
Console.WriteLine(max );
Console.ReadKey();
for循环结构:
01水仙个数
int sum=0;
for (int i = 100; i <= 999; i++)
{
int a = 0, b = 0, c = 0, d = 0;
a += i / 100;
b += i % 100 / 10;
c = i % 100 % 10;
d = a*a*a + b*b*b + c*c*c;
if (d == i)
{
sum += 1;
}
}
Console.WriteLine("水仙个数为{0}", sum);
Console.ReadKey();
显示三角乘法表:
for (int j = 1; j <= 9; j++)
{
for (int i = 1; i <= j; i++)
{
Console.Write("{0}*{1}={2}\t", i, j, i + j);
}
Console.WriteLine();
}
Console.ReadKey();
Break 用于跳出当前循环体,最里面的循环。
int i=0;
while (true)
{
if (i == 5)
{
Console.WriteLine("白总很帅");
break;
}
i++;
}
Console.WriteLine("系统识别此话是真话");
Console.ReadKey();
显示:白总很帅 系统识别此话是真话,后面直接不进行了。