1.while语句:当···的时候
格式:初始条件
while(循环条件)
{
循环体;
状态改变;
}
相当于for循环的另一种变形.
例如:
1 static void Main(string[] args) 2 { 3 //循环四要素:初始条件,循环条件,状态改变,循环体 4 int sum = 0; 5 int i = 1;//初始条件 6 while (i <= 100)//循环条件 7 { 8 sum += i;//循环体 9 i++;//状态改变 10 } 11 Console.Write(sum); 12 }
1 static void Main(string[] args) 2 { 3 //循环四要素:初始条件,循环条件,状态改变,循环体 4 int sum = 0; 5 int i = 1; 6 for (; i <= 100; ) 7 { 8 sum += i; 9 i++; 10 } 11 Console.Write(sum); 12 }
案例一:纸张厚度为0.07毫米,问对折多少次可以超过珠峰8848米。(27次)
1 static void Main(string[] args) 2 { 3 //纸张对折,厚度0.07毫米,折叠多少次超过珠峰 4 5 //定义变量 6 double houdu = 0.00007; 7 int count = 0; 8 9 ////运算 10 while (houdu <= 8848) 11 { 12 13 houdu *= 2; 14 count++; 15 } 16 17 ////输出 18 Console.WriteLine(count); 19 }
案例二:小明老板让他去采购生活用品,牙刷5元,香皂2元,洗发水15元,100元买这三种恰好花光,请问有多少种可能。(44)
1 static void Main(string[] args) 2 { 3 //小明老板让他去采购生活用品,牙刷5元,香皂2元,洗发水15元,100元买这三种恰好花光,请问有多少种可能。 4 int count = 0; 5 int a = 0; 6 while (a<=20) 7 { 8 9 int b = 0; 10 while (b<=50) 11 { 12 int c = 0; 13 while (c<=100/15) 14 { 15 16 if (a*5+b*2+c*15==100) 17 { 18 count++; 19 Console.WriteLine("牙刷{0}个,香皂{1}个,洗发水{2}瓶",a,b,c); 20 21 } 22 c++; 23 } 24 b++; 25 } 26 a++; 27 28 } 29 Console.WriteLine("一共有{0}种可能",count); 30 }
案例三:猴子吃桃子:公园里有一只猴子,和一堆桃子,猴子每天吃完桃子总数的一半,在剩下一半数量中扔掉一个坏的。每天这样吃,到第七天,猴子睁开眼时,发现只剩下一个桃子了,问刚开始公园里有多少个桃子? 190
1 static void Main(string[] args) 2 { 3 //猴子吃桃,每天吃一半,然后扔掉一个坏的,第7天发现只剩一个桃子了,问原来有多少个桃子 4 5 int taozi = 1; 6 int i = 6; 7 while (i>=1) 8 { 9 taozi = (taozi + 1) * 2; 10 i--; 11 } 12 Console.WriteLine(taozi); 13 }
案例四:一堆苹果,3个3个分恰好分完,4个4个分剩余一个,可能有多少个苹果。取前五种可能
1 static void Main(string[] args) 2 { 3 //一堆苹果,3个3个分恰好分完,4个4个分剩余一个,可能有多少个苹果 4 int sum = 0; 5 int i = 1; 6 while (i<=1000) 7 { 8 if(i%3==0&&i%4==1) 9 { 10 sum++; 11 Console.WriteLine("可能有{0}个苹果",i); 12 } 13 14 i++; 15 if (sum> 5) 16 { 17 break; 18 } 19 } 20 21 }
运行结果:
案例五:兔子生兔子:有一对幼兔,幼兔1个月后长成小兔,小兔1个月后长成成兔并生下一对幼兔,问24个月后,有多少对兔子,幼兔,小兔,成兔对数分别是多少。成兔每月生下一对幼兔。
1 static void Main(string[] args) 2 { 3 //兔子生兔子 4 int yt = 1, xt = 0, ct = 0; 5 int i = 1; 6 int sum = 1; 7 while (i<=24) 8 { 9 if (i==1) 10 { 11 yt = 1; 12 xt = 0; 13 ct = 0; 14 } 15 else 16 { 17 ct = xt + ct;//每月成兔对数=上月小兔数+上月成兔数 18 xt = yt;//每月小兔数=上月幼兔数 19 yt = ct;//每月幼兔数=本月成兔数 20 21 } 22 23 sum = xt + yt + ct; 24 25 Console.Write("第{0}个月的幼兔{1}只,小兔{2}只,成兔{3}只 ", i, yt, xt, ct); 26 Console.WriteLine("总数为{0}对", sum); 27 i++; 28 } 29 Console.WriteLine("24个月后总数为{0}对", sum); 30 }
案例六:百鸡百钱:公鸡2文,母鸡1文,小鸡半文,每种至少一只,100文买100只鸡有多少可能性。(33)
1 static void Main(string[] args) 2 { 3 //百鸡百钱:公鸡2文,母鸡1文,小鸡半文,每种至少一只,100文买100只鸡有多少可能性 4 int sum = 0; 5 int a = 1; 6 while (a<=50) 7 { 8 int b = 1; 9 while (b<=100) 10 { 11 int c = 1; 12 while (c<=100) 13 { 14 if (a+b+c==100&&a*2+b*1+c*0.5==100) 15 { 16 sum++; 17 Console.WriteLine("公鸡{0}只,母鸡{1}只,小鸡{2}只",a,b,c); 18 } 19 c++; 20 } 21 b++; 22 } 23 a++; 24 } 25 Console.Write("一共有{0}种可能性",sum); 26 }
案例七:求1-100的和。(5050)
1 static void Main(string[] args) 2 { 3 //求1-100的和 4 int sum = 0; 5 int i = 1; 6 while (i<=100) 7 { 8 sum += i; 9 i++; 10 } 11 Console.WriteLine(sum); 12 }
2.do····while语句
不管下面的while的表达式正确与否,都要先去执行一遍
1 static void Main(string[] args) 2 { 3 int a = 3; 4 do //不管下面的while的表达式正确与否,都要先去执行一遍 5 { 6 a = a - 4; 7 } while (a > 4); 8 9 Console.WriteLine(a);
运行结果:
3.switch ···case
switch case必须与break一同使用。
break 是跳转语句,与switch case 连用的时候是跳出最近的{}。
1 static void Main(string[] args) 2 { 3 Console.WriteLine("1.汉堡包"); 4 Console.WriteLine("2.可口可乐"); 5 Console.WriteLine("3.鸡腿"); 6 Console.Write("请输入你需要的商品序号:"); 7 string a = Console.ReadLine(); 8 switch (a) //小括号内是一个数据类型的值 9 { 10 //case后加空格,之后写上跟上面小括号内对应类型的可能出现的值。 11 12 case "1": //a的值为"1",则进行下面这一步。 13 Console.WriteLine("您选择的是汉堡包!"); 14 break; //距离break最近的大括号,跳出这个大括号,执行大括号之后的命令。 15 16 case "2": 17 Console.WriteLine("您选择的是可口可乐!"); 18 break; 19 case "3": 20 Console.WriteLine("您选择的是鸡腿!"); 21 break; 22 23 default: //如果值跟上面的都不匹配,则进行这一步。 24 Console.WriteLine("您的输入有误!"); 25 break; 26 } 27 28 Console.ReadLine(); 29 }