字符串转数字|continue|三元表达式|随机数产生|枚举|结构体

01.三种方法转换字符串变成数字

<span style="font-size:18px;">//转换数字的三种方法 
            string strNum = Console.ReadLine();
            //int age =int.Parse(strNum);  一种方法
            //int age=Convert.ToInt32(strNum);  二种方法
            int age = 0;
            bool result = int.TryParse(strNum, out age); //三种方法
            if (result)
            {

            }
            Console.WriteLine(result);
            Console.ReadKey();</span>

02.continue

//continue立即结束本次循环 ,判断条件是否成立,成立继续循环
    //用while continue实现计算1到100(含)之间的除了能被7整除之外所有整数的和 
            int i = 0;
            int sum = 0;
            while (i < 100)
            {
                i++;
                if (i % 7 == 0)
                {
                    
                    continue;
                }
                sum += i;
              

            }
            Console.WriteLine("总和为{0}", sum);
            Console.ReadKey();

03.三元表达式

//表达式1?表达式2 :表达式3
            Console.WriteLine("输入姓名");
            string name = Console.ReadLine();
            Console.WriteLine(name == "小杨" ? "系统提示此人很纯洁" : "此人很邪恶");

04.随机数产生

//让用户输入两个数,比较大小  显示最大的那个数
                Console.WriteLine("显示随机数");
                Random r = new Random();
                int num = r.Next(0, 10);
                Console.WriteLine(num);
                Console.ReadKey();

05.枚举

class Program
    {
        public enum Gender
        {
            男,
            女=5
        } 
        static void Main(string[] args)
        {
            //常量--永远不变的量   变量--经常会变的量
            //const int num = 10;
            Gender gender = Gender.男;
            int num = (int)gender;//枚举可以强制转换成int类型,枚举的好处:方便
            //Gender g=(Gender)(Enum.Parse(typeof(Gender),"男"); 
            //字符串转枚举值

            Console.WriteLine (num);
            Console.ReadKey ();           
        }
    }
}

06.结构体

namespace _12结构练习
{
    public enum Gender
    {
        男,女
    }
    public struct Person
    {
        public string _name;
        public int _age;
        public Gender _gender;
    }
    class Program
    {
        static void Main(string[] args)
        {
            Person ZS;
            ZS._gender = Gender.男;

        }
    }
}

07.数组声明方式

//一次性声明多个相同类型的变量---数组
            
            //第一种
            int[] nums1 = new int[10];
            //第二种
            int[] nums2 = new int[]{1,2,3,4,5};
            //第三种
            int[] nums3 = new int[3] { 1, 2, 3 };
            //第四种
            int[] nums4 = { 1, 2, 3, 4, 5 };









   










                                                                     

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 32
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值