频道栏目
首页 > 资讯 > C# > 正文

C#实例之写一个测试标准身高体重的程序、输入日期年月日, 计算这一天是一年中的第几天(考虑闰年)

18-01-18        来源:[db:作者]  
收藏   我要投稿

一、switch 语句练习例子

//.编写一个测试标准身高体重的程序。

//计算公式:

//男人 标准体重(kg)= 身高(cm)-105;

//女人 标准体重(kg)=身高(cm)-100;

//项目要求:

//启动程序,文字提示选择性别。

//(1)男性 (2)女性

//文字提示,输入身高和体重。

//用户输入之后,输出标准体重

//给出评价:

//真实体重/标准体重—》比例

//标准体重60 %以下 严重营养不良

//标准体重60 % ~80 %中度营养不良

//标注体重80 % ~90 %轻度营养不良

//标准体重90 % ~110 %正常

//标准体重110 % ~120 %轻度肥胖

//》标准体重120 %肥胖

Console.WriteLine("选择性别1:男\t2:女");

intgender =int.Parse(Console.ReadLine());

Console.WriteLine("请输入身高");

intheight =int.Parse(Console.ReadLine());

Console.WriteLine("请输入体重");

intweight =int.Parse(Console.ReadLine());

intstanderWeight = 0;

switch(gender)

{

case1:

standerWeight = height - 105;

break;

case2:

standerWeight = height - 100;

break;

default:

Console.WriteLine("不存在此性别");

break;

}

floatrate = weight * 1.0f / standerWeight;//*1.0f,转变成小数,必须加f

if(rate< 0.6f )//f可加可不加

{

Console.WriteLine("严重营养不良");

}

elseif(rate < 0.8f)//否则如果,就包括rate >= 0.6 &&rate <0.8

{

Console.WriteLine("中度营养不良");

}

elseif(rate < 0.9)

{

Console.WriteLine("轻度营养不良");

}

elseif( rate < 1.1)

{

Console.WriteLine("正常");

}

elseif(rate < 1.2)

{

Console.WriteLine("轻度肥胖");

}

else

{

Console.WriteLine("肥胖");

}

二、goto跳转语句

inttimes = 0;

label:

times++;

Console.WriteLine("hello world"+ times);

gotolabel;

例子1:

//输入一个日期年月日,计算这一天是一年中的第几天(考虑闰年)

1、 //第二种方法

Console.WriteLine("输入年份");

intyear =int.Parse(Console.ReadLine());

Console.WriteLine("输入月份");

intmonth =int.Parse(Console.ReadLine());

Console.WriteLine("输入日期");

intday =int.Parse(Console.ReadLine());

//记录累加的天数

inttotalDays = 0;

switch(month)

{

case12:

totalDays += 30;

gotocase11;

case11:

totalDays += 31;

gotocase10;

case10:

totalDays += 30;

gotocase9;

case9:

totalDays += 31;

gotocase8;

case8:

totalDays += 31;

gotocase7;

case7:

totalDays += 30;

gotocase6;

case6:

totalDays += 31;

gotocase5;

case5:

totalDays += 30;

gotocase4;

case4:

totalDays += 31;

gotocase3;

case3:

totalDays += 28;//判断是否是闰年;

if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)

{

totalDays++;

}

gotocase2;

case2: // //加上一月份的天数再到case1里加上输入的天数,就是第几天

totalDays += 31;

gotocase1;

case1: //必须倒着写,因为这个有break;

totalDays += day;

break;

default:

break;

}

Console.WriteLine("{0}年{1}月{2}日是今年的第{3}天", year, month, day, totalDays);

2、第一种方法:用if else语句实现的。(上面的程序功能)

Console.WriteLine("请输入年份");

intyear =int.Parse(Console.ReadLine());

Console.WriteLine("请输入月份");

intmonth =int.Parse(Console.ReadLine());

Console.WriteLine("请输入日期");

intday =int.Parse(Console.ReadLine());

//记录累加的天数

//2 20

inttotalDays = 0;

totalDays += day;

month -= 1;

if(month==11)

{

totalDays += 30;

month -= 1;

}

if(month == 10)

{

totalDays += 31;

month -= 1;

}

if(month == 9)

{

totalDays += 30;

month -= 1;

}

if(month == 8)

{

totalDays += 31;

month -= 1;

}

if(month == 7)

{

totalDays += 31;

month -= 1;

}

if(month == 6)

{

totalDays += 30;

month -= 1;

}

if(month == 5)

{

totalDays += 31;

month -= 1;

}

if(month == 4)

{

totalDays += 30;

month -= 1;

}

if(month == 3)

{

totalDays += 31;

month -= 1;

}

//能执行下面的if条件的 肯定是大于2月份的

if(month == 2)

{

totalDays += 28;

month -= 1;

//判断是否为闰年 是闰年再加1

if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0)

{

totalDays++;

}

}

if(month == 1)

{

totalDays += 31;

month -= 1;

}

Console.WriteLine("这是今年的第{0}天",totalDays);

三、 switch (day)

{

case 1: //这几个条件相同的前提下,可简写,可执行最后一个,

case 2:

case 3:

case 4:

case 5:

Console.WriteLine("今天工作日");

四、//使成绩分A、B、C、D、E等级

Console.WriteLine("请输入学员成绩");

intscore =int.Parse(Console.ReadLine());

charlevel ='A';

switch(score / 10)

{

case10:

case9:

level = 'A';

break;

case8:

level = 'B';

break;

case7:

level = 'C';

break;

case6:

level = 'D';

break;

default:

level = 'E';

break;

}

Console.WriteLine(level);

相关TAG标签
上一篇:编程开发Pascal's Triangle II解析
下一篇:编程开发Pascal's Triangle解析
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站