C#学习之旅--Day03-循环语句-跳转语句-语法

一、循环语句

1.for

for (初始化; 循环条件;增减变量)
{
    循环体
}

满足循环条件作循环 顺序:初始,判断 执行 增减变量 判断 执行 增减变量……

for (int i = 0; i < 5; i++)
{
    Console.WriteLine(i);
}

练习:一张纸的厚度为0.01毫米 请计算对折30次以后的厚度为多少米

float thickness = 0.00001f;

for (int i = 0; i < 30; i++)
{
    thickness *= 2;
}
Console.WriteLine("纸的厚度为:" + thickness);

2.while

while(条件)

{
      循环体
 }

满足条件一直循环

int j = 0;
while (j < 5)
{
    Console.WriteLine(j);
    j++;
}

练习:一个球从100米高度落下,每次落地后,弹回原高度一半。计算,总共经过?次最终落地。下次弹跳高度<0.01米不再起跳。总共经过多少米

int num = 0;
float height = 100;
float sum1 = height;
while (height / 2 > 0.01f)
{
    height *= 0.5f;
    sum1 += height * 2;
    num++;
}
Console.WriteLine("总共经过{0}次落地,共经过{1:f1}米", num, sum1);

3.do while

 do{
循环体
}while(条件);

先执行一次循环,再判断是否需要循环

练习:猜数字
            //程序产生1-100的随机数
            //让玩家重复猜测,直到猜对位置
            //返回"大了""小了""恭喜猜对了,总共猜了多少次"

//创建一个随机数工具
Random random = new Random();
//产生一个随机数
int numberG = random.Next(1, 101);//101取不到,1取得到

 
int count = 0;
do
{
    int number = int.Parse(Console.ReadLine());
    count++;
    if (number == numberG)
    {
        Console.WriteLine("恭喜猜对了,总共猜了{0}次", count);
        break;
    }
    else if (number < numberG) Console.WriteLine("小了");
    else Console.WriteLine("大了");
} while (true);

二、跳转语句

1.continue

结束本次循环 继续下次循环

int sum = 0;
for (int i = 0; i <= 100; i++)
{
    if (i % 3 != 0) continue;//跳过不能被3整除的数字
    sum += i;
}

2.break

退出循环


3.return

退出方法

三、语法

1.定义方法

[访问修饰符][可选修饰符] 返回类型 方法名称(参数列表)
         {
             方法体
             return
         )

private static void Fun1()
{
    Console.WriteLine("Fun1执行了");
}

返回类型 int double float string

 void 空的 没有返回类型 retrun可有可无

private static int Fun2()
{
    Console.WriteLine("Fun2执行了");
    return 250;
}

参数设定

private static void Fun3(int a,string b) //形参
{  
    Console.WriteLine(a);
}
//方法内只应该有【一个】功能
//用的人提供数据
private static int Add(int a , int b)
{
    return a + b;
}
static void Main4()
{
    //调用方法
    //方法名称(参数)
    Fun1();
    int num = Fun2();

    //传参
    int a = 100;
    string b = "ok";
    Fun3(a, b); //实参 与形参一一对应  吧
}

练习:

1.在控制台中显示年历的方法*--调用12遍显示月历
2.在控制台中显示月历的方法
--显示表头console.WriteLine(""日\t—t二lt......"");
--计算当月1日星期数,输出空白(t)
   Csonsole.Write("\t"")
--计算当月天数,输入1\t 2 3 4-每逢周六换行
3.根据年月日,计算星期数[赠送]
4.计算指定月份的天数
5.判断闰年的方法
--2月闰年29天平年28天
--年份能被4整除但是不能被100整除年份能被400整除

 /// <summary>
 /// 根据年份判断是闰年还是平年并返回真假,真为闰年,假为平年
 /// </summary>
 /// <param name="year">年份</param>
 /// <returns>真假</returns>
 private static bool IsLeapYear(int year)
 {
     //if ((year % 4 ==0 && year %100 != 0) || year % 400 == 0) return true;
     //else return false;
     return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
 }

 /// <summary>
 /// 根据指定月份计算当月天数
 /// </summary>
 /// <param name="year">年</param>
 /// <param name="month">月</param>
 /// <returns>该月天数</returns>
 private static int GetDaysByMonth(int year,int month) 
 {
     if (month < 1 && month > 12) return 0;//短路(错误)放上面 用return跳出

     switch (month)
     {
         case 2:
             //if (IsLeapYear(year))
             //    return 29;
             //else 
             //    return 28;
             return IsLeapYear(year) ? 29 : 28; //用bool判断的时候就考虑是否三元可以
         case 4:
         case 6:
         case 9:
         case 11:
             return 30;
         default:
             return 31;
     }
 }

 /// <summary>
 /// 根据年月日,计算星期数的方法
 /// </summary>
 /// <param name="year">年份</param>
 /// <param name="month">月份</param>
 /// <param name="day">天</param>
 /// <returns>星期数</returns>
 private static int GetWeekByDay(int year,int month,int day)
 {
     DateTime dt = new DateTime(year,month,day);
     return (int)dt.DayOfWeek;
 }

 /// <summary>
 /// 根据年月显示月历
 /// </summary>
 /// <param name="year">年</param>
 /// <param name="month">月</param>
 private static void MonthCalendar(int year,int month)
 {
     //1、显示表头
     Console.WriteLine("{0}年{1}月", year, month);
     Console.WriteLine("日\t一\t二\t三\t四\t五\t六"); 
     
     
     //2、根据1日星期数,显示空白
     int weekByDay01 = GetWeekByDay(year, month, 1);
     for (int j = 1; j < weekByDay01; j++)
         Console.Write("\t");

     //3、根据月份总天数,显示日
     int day = GetDaysByMonth(year, month);
     for (int i = 1;i< day + 1; i++)
     {
         Console.Write(i + "\t");
         //逢周六换行
         if (GetWeekByDay(year, month, i) == 6) 
             Console.WriteLine();
         //if (weekByDay == 6)
         //{
         //    Console.Write(i + " \n");
         //}
         //else Console.Write(i + "\t");
     }
 }

 /// <summary>
 /// 根据年份显示年历
 /// </summary>
 /// <param name="year">年</param>
 private static void YearCalendar(int year)
 {
     for (int i = 1; i < 13; i++)
     {
         MonthCalendar(year,i);
         Console.WriteLine();
     }
 }

 static void Main()
 {
     Console.WriteLine("请输入年份:");
     int year = int.Parse(Console.ReadLine());
     YearCalendar(year);
 }

  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

秦果

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值