C# study2

知识点:

c#的数据类型分为两类:
1.值类型:基本数据类型  枚举类型  结构类型
  a.值类型的变量是保存栈区
  b.值类型的变量存储是实际数据
         
2.引用类型:类  接口 委托  数组
  a.引用类型是保存在堆区
  b.引用类型存储不是实际数据,存储的是引用(地址)

装箱和拆箱:
1.装箱:把值类型转换为引用类型
2.拆箱:把引用类型转换为值类型,拆箱之前必须先装箱

变量:可以变化的量,就是在内存开辟了一个合适的存储空间,只是该空间保存的数据可以发生改变
注意:局部变量在使用前必须先声明并赋值
声明格式:
数据类型  变量名;  //只是声明变量,并没有赋值
数据类型  变量名=值;  //声明变量并初始化值

常量:不给修改
格式:  const 常量名=值
声明常量时必须要初始化值,之后就不能再修改


c#语言的三种流程语句结构:
1.顺序结构
2.分支结构(条件判断):if switch
  if语句:
  a.单分支语句
     if(条件){
    条件成立时执行
}
    注意:{}可以省略,条件成立时,默认与if最近的语句是{}中的语句,建议不管是多条语句还是一条语句,都加上{}
  b.双分支:
     if(条件)}{
    条件成立时执行
}   
     else{
    条件不成立时执行
}
  c.多分支:
     if(条件1)}{
    条件1成立时执行
}   
     else if(条件2){
    条件2成立时执行
}
else if(条件3){
    条件2成立时执行
}
....
else{
 
}

  switch语句:开关语句
  格式:
  switch(表达式){
     case 值1:
     语句1;
 break;
case 值2:
     语句2;
 break;
.....
default
    默认语句;
break;
  }
  switch注意事项:
  a.表达式的类型只能是整数 char  string 枚举
  b.当表达式的值等于对应的case时,就会执行case后的语句,碰到break才会退出switch语句
  c.case后的值不能有相同的
  d.case与default的顺序无关
  e.表达式的值与case后的值要一致
 
3.循环结构
while循环
    while(表达式){
  循环体
}
分析步聚:
   a.要执行的条件
   b.哪些需要循环执行
   c.退出循环的条件

do...while循环:
  do{
       循环体
  }while(表达式);


while与do..while的区别:
1.while是先判断再执行,所以可能一次循环都不会执行
2.do...while 是先执行再判断,至少会执行一次

for循环:
for(初始化条件;条件判断;改变循环的条件){
   循环体;
}
for注意事项:
1. 初始化条件在整个循环中只会执行一次
2.for()中的两个分号不能省,其它可以省略

break:  结束整个循环
continue: 结束当次循环


练习:

1.计算圆面积:程序要求输入半径,输入完成后程序将计算出圆的面积,并输出到屏幕上。

基本思路:

(1)定义一个double类型的常量,用于存放PI的值。
(2)程序提示“要求输入圆的半径”。
(3)把输入的字符串数据装换成float型数据。 
(4)程序输出所求圆的面积。

代码1:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
class w1
{
    static void Main()
    {
        const float PI = 3.1415f;
        Console.Write("请输入圆的半径:R =");
        string x = Console.ReadLine();
        float r = float.Parse(x);
        double s = PI * r * r;
        Console.WriteLine("半径为" + r + "的圆的面积为" + s);
    }
}


2.输入今天是星期几:如果输入的是1-5,打印:工作日快乐;  输入6,7,则打印:周末愉快;输入的不是1-7之间, 则打印:输入有误,要求:用多重if结构或者switch结构。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace work2
{
    class Program
    {
        static void Main(string[] args)
        {
            int day = int.Parse(Console.ReadLine()); 
           /* switch (day)
            {
                case 1:
                case 2:
                case 3:
                case 4:
                case 5:
                    Console.WriteLine("工作日快乐!");
                    break;
                case 6:
                case 7:
                    Console.WriteLine("周木愉快!");
                    break;
                default:
                    Console.WriteLine("输入有误!");
                    break;
            }*/

            if (day >= 1 && day <= 5)
                Console.WriteLine("工作日快乐!");
            else if (day >= 6 && day <= 7)
                Console.WriteLine("周木愉快!");
            else
                Console.WriteLine("输入有误!");
        }
    }
}
3..打印九九乘法表。

代码3:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace w1
{
    class Program
    {
        static void Main(string[] args)
        {
            int i,j,res;
            for (i = 1; i <= 9; i++)
            {
                for (j = 1; j <= i; j++)
                    Console.Write("i*j=" + (i * j)+" ");
            }
        }
    }
}

4.判断一个五位数是否为回文数。
  提示:所谓回文数就是一个数的所有位对称,如12321就是回文数,  23432和45654也是回文数,输入一个数,判断是不是回文数。

代码4:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace work3
{
    class Program
    {
        static void Main(string[] args)
        {
            int a, b, c, d, e;
            bool flag =false;
            int x = int.Parse(Console.ReadLine());
            if (x >= 10000 && x <= 99999)
            {
                a = x / 10000;
                b = x % 10000 / 1000;
                d = x%100/10;
                e = x % 10;
                if (a == e && b == d)
                    flag=true;
               // Console.WriteLine("a=" + a +" b=" + b +" d=" + d + " e="  +e+ " f=" + f);
            }
            if (flag)
                Console.WriteLine(x + "是五位数的回文数。");
            else Console.WriteLine(x + "不是五位数的回文数。");
        }
    }
}
5.实现三个数的排序:
从键盘输入三个数,然后对这三个数进行排序(从小到大),并输出。

代码5:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace work4
{
    class Program
    {
        static void Main(string[] args)
        {
            int t;
            int a = int.Parse(Console.ReadLine());
            int b = int.Parse(Console.ReadLine());
            int c = int.Parse(Console.ReadLine());
            if (a > b)
            {
                t = a;
                a = b;
                b = t;
            }
            if (c < a)
                Console.WriteLine(c + " " + a + " " + b);
            else if(c>b)
                Console.WriteLine(a + " " + b + " " + c);
            else
                Console.WriteLine(a + " " + c + " " + b);
        }
    }
}




  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值