
正文
while, do-while ,switch···case语句的学习与运用
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
1.while语句:当···的时候
格式:初始条件
while(循环条件)
{
循环体;
状态改变;
}
相当于for循环的另一种变形.
例如:
static void Main(string[] args)
{
//循环四要素:初始条件,循环条件,状态改变,循环体
int sum = ;
int i = ;//初始条件
while (i <= )//循环条件
{
sum += i;//循环体
i++;//状态改变
}
Console.Write(sum);
}
static void Main(string[] args)
{
//循环四要素:初始条件,循环条件,状态改变,循环体
int sum = ;
int i = ;
for (; i <= ; )
{
sum += i;
i++;
}
Console.Write(sum);
}
案例一:纸张厚度为0.07毫米,问对折多少次可以超过珠峰8848米。(27次)
static void Main(string[] args)
{
//纸张对折,厚度0.07毫米,折叠多少次超过珠峰 //定义变量
double houdu = 0.00007;
int count = ; ////运算
while (houdu <= )
{ houdu *= ;
count++;
} ////输出
Console.WriteLine(count);
}
案例二:小明老板让他去采购生活用品,牙刷5元,香皂2元,洗发水15元,100元买这三种恰好花光,请问有多少种可能。(44)
static void Main(string[] args)
{
//小明老板让他去采购生活用品,牙刷5元,香皂2元,洗发水15元,100元买这三种恰好花光,请问有多少种可能。
int count = ;
int a = ;
while (a<=)
{ int b = ;
while (b<=)
{
int c = ;
while (c<=/)
{ if (a*+b*+c*==)
{
count++;
Console.WriteLine("牙刷{0}个,香皂{1}个,洗发水{2}瓶",a,b,c); }
c++;
}
b++;
}
a++; }
Console.WriteLine("一共有{0}种可能",count);
}
案例三:猴子吃桃子:公园里有一只猴子,和一堆桃子,猴子每天吃完桃子总数的一半,在剩下一半数量中扔掉一个坏的。每天这样吃,到第七天,猴子睁开眼时,发现只剩下一个桃子了,问刚开始公园里有多少个桃子? 190
static void Main(string[] args)
{
//猴子吃桃,每天吃一半,然后扔掉一个坏的,第7天发现只剩一个桃子了,问原来有多少个桃子 int taozi = ;
int i = ;
while (i>=)
{
taozi = (taozi + ) * ;
i--;
}
Console.WriteLine(taozi);
}
案例四:一堆苹果,3个3个分恰好分完,4个4个分剩余一个,可能有多少个苹果。取前五种可能
static void Main(string[] args)
{
//一堆苹果,3个3个分恰好分完,4个4个分剩余一个,可能有多少个苹果
int sum = ;
int i = ;
while (i<=)
{
if(i%==&&i%==)
{
sum++;
Console.WriteLine("可能有{0}个苹果",i);
} i++;
if (sum> )
{
break;
}
} }
运行结果:

案例五:兔子生兔子:有一对幼兔,幼兔1个月后长成小兔,小兔1个月后长成成兔并生下一对幼兔,问24个月后,有多少对兔子,幼兔,小兔,成兔对数分别是多少。成兔每月生下一对幼兔。
static void Main(string[] args)
{
//兔子生兔子
int yt = , xt = , ct = ;
int i = ;
int sum = ;
while (i<=)
{
if (i==)
{
yt = ;
xt = ;
ct = ;
}
else
{
ct = xt + ct;//每月成兔对数=上月小兔数+上月成兔数
xt = yt;//每月小兔数=上月幼兔数
yt = ct;//每月幼兔数=本月成兔数 } sum = xt + yt + ct; Console.Write("第{0}个月的幼兔{1}只,小兔{2}只,成兔{3}只\t", i, yt, xt, ct);
Console.WriteLine("总数为{0}对", sum);
i++;
}
Console.WriteLine("24个月后总数为{0}对", sum);
}
案例六:百鸡百钱:公鸡2文,母鸡1文,小鸡半文,每种至少一只,100文买100只鸡有多少可能性。(33)
static void Main(string[] args)
{
//百鸡百钱:公鸡2文,母鸡1文,小鸡半文,每种至少一只,100文买100只鸡有多少可能性
int sum = ;
int a = ;
while (a<=)
{
int b = ;
while (b<=)
{
int c = ;
while (c<=)
{
if (a+b+c==&&a*+b*+c*0.5==)
{
sum++;
Console.WriteLine("公鸡{0}只,母鸡{1}只,小鸡{2}只",a,b,c);
}
c++;
}
b++;
}
a++;
}
Console.Write("一共有{0}种可能性",sum);
}
案例七:求1-100的和。(5050)
static void Main(string[] args)
{
//求1-100的和
int sum = ;
int i = ;
while (i<=)
{
sum += i;
i++;
}
Console.WriteLine(sum);
}
2.do····while语句
不管下面的while的表达式正确与否,都要先去执行一遍
static void Main(string[] args)
{
int a = ;
do //不管下面的while的表达式正确与否,都要先去执行一遍
{
a = a - ;
} while (a > ); Console.WriteLine(a);
运行结果:

3.switch ···case
switch case必须与break一同使用。
break 是跳转语句,与switch case 连用的时候是跳出最近的{}。
static void Main(string[] args)
{
Console.WriteLine("1.汉堡包");
Console.WriteLine("2.可口可乐");
Console.WriteLine("3.鸡腿");
Console.Write("请输入你需要的商品序号:");
string a = Console.ReadLine();
switch (a) //小括号内是一个数据类型的值
{
//case后加空格,之后写上跟上面小括号内对应类型的可能出现的值。 case "": //a的值为"1",则进行下面这一步。
Console.WriteLine("您选择的是汉堡包!");
break; //距离break最近的大括号,跳出这个大括号,执行大括号之后的命令。 case "":
Console.WriteLine("您选择的是可口可乐!");
break;
case "":
Console.WriteLine("您选择的是鸡腿!");
break; default: //如果值跟上面的都不匹配,则进行这一步。
Console.WriteLine("您的输入有误!");
break;
} Console.ReadLine();
}







