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 };