zoukankan      html  css  js  c++  java
  • 2012年3月5日c#基础复习练习

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

    namespace 函数
    {
        class Program
        {
           
           
            static void Main(string[] args)
            {   //练习1利用函数计算两个数的和
                /*
                Console.WriteLine(Max(2,5));
                Console.ReadKey();
                */
                //练习2 利用函数计算数组的和
                /*
                int[] values = { 2,3,4,5,6,2,4,5,2,1,4,5};
                int sum = values.Sum();还可以使用效果int sum=Sum(values); 是一样的 
                Console.WriteLine(sum);
                Console.ReadKey();
                */
                //练习 3 用“|”分割数组,并形成新的字符串
                /*
                string[] name = { "aa","bb","cc"};
                string newstrings = Join(name,"|");
                Console.WriteLine(newstrings);
                 */
                //练习4 可变参数
                /*
                Hey("a","b","c","d","e","f","g");
                Sayhello("大帅哥","sad","sd","dd");//可以与非可变参数合用,如果不是可变参数,就得使用重载,可变参数必须放在形参最后一个参数
                 */
                //练习5 函数重载,函数名相同,函数的类型和个数不同
                /*
                Hello();
                Hello("小明");
                Hello("小明",8);
                 */
                //练习6  字符串练习
                /*
                string s="HHH";
                Console.WriteLine(s.ToLower());//转换成小写
                Console.WriteLine(s.ToUpper());//转换成大写
                string b = "    a   b   ";
                Console.WriteLine(b.Trim());//它只会去掉2边的空白,中间的它不管
                bool c = s.Equals("hhh",StringComparison.OrdinalIgnoreCase);//字符串比较,ignore忽略 case 大小写,==是区分大小写的比较
                Console.WriteLine(c);
                string n="sss,ss,ff,fg,sd,aws,sdf,tht,xc|ttt[fassaf]fasdsf";
                string[] name = n.Split(',');//用字符,进行分割字符串
                name = n.Split(',','|','[',']');//是可变参数,可以利用多个分割符进行拆分
                foreach(string i in name)
                {
                    Console.Write(i);
                }
                string j = "tht,xc|ttt[fassaf]fas,,,,,,,,dsf";
                string [] hello = j.Split(new char[]{ ',' }, StringSplitOptions.RemoveEmptyEntries);//用,分割字符串,并清除字符串中的空字符
                foreach (string p in hello)
                {
                    Console.WriteLine(p);
                }
                string k = "小狗 小猫  小鸡 小兔 小虫子 小人 小花 小草";
                string[] kk = k.Split(new string[] {"小"}, StringSplitOptions.RemoveEmptyEntries);//这是字符串的使用形式
                foreach (string g in kk)
                {
                    Console.WriteLine(g);
                }
                 */
                //练习7 字符串处理
                /*
                string emal = Console.ReadLine();
                int atindex = emal.IndexOf('@');
                string name = emal.Substring(0, atindex);
                string aftername = emal.Substring(atindex+1);
                Console.WriteLine("{0}{1}",name,aftername);
                 */
                //练习8 聊天机器人
                /*
                Console.WriteLine("你好我是机器人");
                int full = 2;
                while(true)
                {  
                    if(full<=0)
                    {
                        Console.WriteLine("我快饿死了,没法聊了,要想聊,先喂饭,聊几次喂几次");
                        full = Convert.ToInt32(Console.ReadLine());
                        if(full<=0)
                        {
                            Console.WriteLine("玩我呢?生气了");
                            return;
                        }
                    }
                    Console.WriteLine("你想说什么呢?");
                    string say = Console.ReadLine();
                    if(say.Contains("天气"))
                    {
                        say = say.Substring(2);
                        Console.WriteLine("{0}天气不好",say);
                    }
                    else if(say.Contains("88")||say.Contains("再见"))
                    {
                        Console.WriteLine("再见");
                        return;
                    }
                    full--;
                }
                 */
                //练习9 ref out 参数应用
                int age = 20;//ref 是引用必须先初始化
                Intage(ref age);
                Console.WriteLine(age);//结果是21,没有应用ref参数的话结果还20,是复制不是引用,用了ref就是引用,还需注意ref需配对使用
                int outage;//out 不需要初始话,初始话了也没用,必须在函数内赋值,是内部为外部赋值,out一般用在函数有多个返回值的场所
                Outage( out outage);
                Console.WriteLine(outage);
                string str = Console.ReadLine();
                int i;
                if (int.TryParse(str, out i))
                {
                    Console.WriteLine("转换成功{0}", i);
                }
                else
                {
                    Console.WriteLine("转换失败");
                }
                int i1 = 10; int i2 = 20;
                swap(ref i1,ref i2);
                Console.WriteLine("{0}{1}",i1,i2);
                Console.ReadKey();

            }
            static void swap(ref int i1, ref int i2)
            {
                int temp = i1;
                i1 = i2;
                i2 = temp;
            }
            static void Outage(out int outage)
            {
                 outage = 80;
            }
            static void Intage(ref int age)
            {
                age++;
            }
            static void Hello()
            {
                Console.WriteLine("你好") ;
            }
            static void Hello(string name)
            {
                Console.WriteLine("{0}你好",name);
            }
            static void Hello(string name ,int age)
            {
                Console.WriteLine("{0}你好!你已经{1}岁了",name,age);
            }
            static void Sayhello(string name, params string[] names)
            {
                Console.WriteLine("我的名字是{0}",name);
                foreach(string nickname in names)
                {
                    Console.WriteLine("我的昵称是{0}",nickname);
                }
            }
            static void Hey(params string [] names)
            {
                foreach(string name in names)
                {
                    Console.WriteLine(name);
                }
            }
            static string Join(string [] name,string sperate)
            {
                string s = "";
                for (int i = 0; i < name.Length - 1;i++ )
                {
                    s=s+name[i]+sperate;
                }
                if(name.Length>0)
                {
                    s=s+name[name.Length-1];
                }
                return s;
            }
            static int Sum(int [] values)
            {
                int sum = 0;
                foreach(int i in values)
                {
                    sum = sum + i;
                }
                return sum;
            }
            static int Max(int i1,int i2)//计算2个数最大值
            {
                if(i1>i2)
                {
                    return i1;
                }
                else
                {
                    return i2;
                }
            }
        }
    }

  • 相关阅读:
    View转化为bitmap
    Bitmap 与Drawable相互转换
    android studio 连接不到真机
    解决Android sync无法同步问题
    Stetho管理手机
    android sugar no such table
    android 建数据库的正确写法
    android JSON 数据解析
    android notification 理解
    android aidl 简单使用
  • 原文地址:https://www.cnblogs.com/liujiaoxian/p/csharpe.html
Copyright © 2011-2022 走看看