您的位置:首页 > 其它

while, do-while ,switch···case语句的学习与运用

2016-04-19 20:44 495 查看
1.while语句:当···的时候

格式:初始条件

while(循环条件)

{

循环体;

状态改变;

}

相当于for循环的另一种变形.

例如:

static void Main(string[] args)
{
//循环四要素:初始条件,循环条件,状态改变,循环体
int sum = 0;
int i = 1;//初始条件
while (i <= 100)//循环条件
{
sum += i;//循环体
i++;//状态改变
}
Console.Write(sum);
}


static void Main(string[] args)
{
//循环四要素:初始条件,循环条件,状态改变,循环体
int sum = 0;
int i = 1;
for (; i <= 100; )
{
sum += i;
i++;
}
Console.Write(sum);
}


案例一:纸张厚度为0.07毫米,问对折多少次可以超过珠峰8848米。(27次)

static void Main(string[] args)
{
//纸张对折,厚度0.07毫米,折叠多少次超过珠峰

//定义变量
double houdu = 0.00007;
int count = 0;

////运算
while (houdu <= 8848)
{

houdu *= 2;
count++;
}

////输出
Console.WriteLine(count);
}


案例二:小明老板让他去采购生活用品,牙刷5元,香皂2元,洗发水15元,100元买这三种恰好花光,请问有多少种可能。(44)

static void Main(string[] args)
{
//小明老板让他去采购生活用品,牙刷5元,香皂2元,洗发水15元,100元买这三种恰好花光,请问有多少种可能。
int count = 0;
int a = 0;
while (a<=20)
{

int b = 0;
while (b<=50)
{
int c = 0;
while (c<=100/15)
{

if (a*5+b*2+c*15==100)
{
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 = 1;
int i = 6;
while (i>=1)
{
taozi = (taozi + 1) * 2;
i--;
}
Console.WriteLine(taozi);
}


案例四:一堆苹果,3个3个分恰好分完,4个4个分剩余一个,可能有多少个苹果。取前五种可能

static void Main(string[] args)
{
//一堆苹果,3个3个分恰好分完,4个4个分剩余一个,可能有多少个苹果
int sum = 0;
int i = 1;
while (i<=1000)
{
if(i%3==0&&i%4==1)
{
sum++;
Console.WriteLine("可能有{0}个苹果",i);
}

i++;
if (sum> 5)
{
break;
}
}

}


运行结果:



案例五:兔子生兔子:有一对幼兔,幼兔1个月后长成小兔,小兔1个月后长成成兔并生下一对幼兔,问24个月后,有多少对兔子,幼兔,小兔,成兔对数分别是多少。成兔每月生下一对幼兔。

static void Main(string[] args)
{
//兔子生兔子
int yt = 1, xt = 0, ct = 0;
int i = 1;
int sum = 1;
while (i<=24)
{
if (i==1)
{
yt = 1;
xt = 0;
ct = 0;
}
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 = 0;
int a = 1;
while (a<=50)
{
int b = 1;
while (b<=100)
{
int c = 1;
while (c<=100)
{
if (a+b+c==100&&a*2+b*1+c*0.5==100)
{
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 = 0;
int i = 1;
while (i<=100)
{
sum += i;
i++;
}
Console.WriteLine(sum);
}


2.do····while语句

不管下面的while的表达式正确与否,都要先去执行一遍

static void Main(string[] args)
{
int a = 3;
do   //不管下面的while的表达式正确与否,都要先去执行一遍
{
a = a - 4;
} while (a > 4);

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 "1": //a的值为"1",则进行下面这一步。
Console.WriteLine("您选择的是汉堡包!");
break; //距离break最近的大括号,跳出这个大括号,执行大括号之后的命令。

case "2":
Console.WriteLine("您选择的是可口可乐!");
break;
case "3":
Console.WriteLine("您选择的是鸡腿!");
break;

default: //如果值跟上面的都不匹配,则进行这一步。
Console.WriteLine("您的输入有误!");
break;
}

Console.ReadLine();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: