c#基础03
变量三要素
变量类型:规定变量的数据类型
变量名:有意义,好记忆
变量值:变量对应的内存数据
数据类型
int:整型
double:浮点型
string:用于存储一段字符
bool:用于表示一个条件是否成立
datetime:用于存储日期和时间
变量声明和赋值
int age ;
age=20;
//声明的同时给变量赋值
string name =“小红”;
//变量的使用
Console.WriteLine(“我叫”+name+“今年”+age+“岁”);
变量命名规范
- 命名尽量用对应的英文命名,比如年龄使用age,除循环变量外尽量不用单个字符
- 严格区分大小写
- 驼峰命名法:当使用一个或者多个单词组成变量名时,要使用camel命名法,即第一个单词的首字母小写,其它单词的首字母大写,如stuName。
- 常量的定义语法:
const 数据类型 常量名称=值;
const double PAI=3.14; - 枚举
访问修饰符(一般public)enum 枚举名{值1,值2…}
- 枚举中不能包含方法,一般在类的外部
console类的使用
Console.WriteLine()//换行
Console.WriteLine("内容")//输出内容换行
Console.Write()//输出内容不换行
字符串格式化
Console.WriteLine("姓名:{0} 年龄:{1}",stuName,stuAge);
强制转换
- 字符串和值转换
int stuAge =int.Parse(Console.ReadLine());
double a=double.parse(“20.5”);
float b=float.parse(“20.25”);
int c =int.pase(“20”); - 值类型强转换成字符串类型
string aa=a.tostring();
string bb=b.tostring();
strign cc=c.tostring(); - 数值跟数值之间转换
double a=2.31;
int b=20;
int result=(int)a+b; - 使用万能转换器进行不同类型的转换
double a=convert.todouble(“20.21”);
float b=convert.tosingle(“20.55”);
int c =convert.toint32(“20”);
DateTime datetime=convert.toDateTime(“2023-02-02”);
int d=convert.toint32(a);
int e=convert.toint32(b);
double f=convert.todouble©; - 两次强制转换
从浮点类型的字符串到int需要两次强制转换
int a=(int)double.parse(“35.34”);
int b=(int)convert.todouble(“34.33”);
快捷键
consol.writeline()//cw按两个tab键
if选择结构
int a= 10;
int b=20;
bool result;
if(a<b)
result=true;
else
result=false;
console.writeline("a>b的比较结果是"+result);
三元运算符
int a=10;
int b=20;
bool result=a>b?true:false;
console.writeline ("a>b的比较结果是:"+result);
if嵌套结构
static void main(stirng []args)
{
console.writeline("请输入客户消费信息:");
int totomaoney =int.parse(console.readline());
if(totalMoney>=1000)
{
console.writeline("需付款:"+tottalmoney*0.8);
conosle.wirte("您的会员类型:");
string customerType=console.readline();
if(customerType=="普通")
{
console.write(同时送您100代金券)
}
else if(customerType=="vip"){
console.wirteline("同时送您200代金券");
}
}
else{
console.wirteline("需付款:"+totalmoney);
}
}
switch 选择结构
常用的语法结构
switch(表达式)
{
case 常量1:
语句;
break;
case 常量2;
语句;
break;
......
defalt://如果没有找到匹配结果
语句;
break;
}
例子:
static viod main(stirng[]args)
{
console.writeline("请输入您购买的电器品牌:");
string band=console.readline();
switch (band)
{
case"A":
console.writeline("赠送您热水器一台");
break;
case"B":
consle.wirteline("赠送手机一部");
break;
case"C":
console.wirteline("赠送音箱一个");
break;
default:
console.witeline("无赠品");
break;
}
console.readline();
}
for循环
class Program
{
static void Main(string[] args)
{
for (int a=1; a <= 9; a++){
for (int b=1; b <= a; b++){
Console.Write("{0}*{1}={2}\t", a, b, a * b);
}
Console.WriteLine();
}
Console.ReadLine();
}
}
while循环
break跳出整个循环
continue跳出
static void Main(string[] args)
{
int count = 0;
while (count < 5)
{
Console.WriteLine("当前队员的成绩:");
int score = int.Parse(Console.ReadLine());
if (score < 60)
{
continue;
}
count++;
Console.WriteLine("该队员姓名:");
Console.ReadLine();
}
Console.WriteLine("队员已招满!");
Console.ReadLine();
}
调试
- 插入断点(双击所在行的侧边)
- 单行运行:F11
- 逐过程运行:F10
- 停止调试:shift+F5
程序调试的思路和详细步骤
- 设置断点::分析可能出现错误的位置,并设置断点
- 调试运行:启动调试,单步运行
- 观察变量:不断观察特定变量的值
- 分析问题:通过观察变量的值,发现问题
- 修改代码:重新运行
字符串的处理
length使用
字符串的长度
static void Main(string[] args)
{
string usrName = "123aga";
int pwdLength = usrName.Length;
Console.WriteLine(pwdLength);
Console.ReadLine();
}
比较字符串内容是否相等
使用"=="或者"equals()"方法,优先使用equals()
static void Main(string[] args)
{
string name1 = "iris";
string name2 = "iris";
string name3 = "tom";
bool a = name1.Equals(name2);
bool b =name1.Equals(name3);
Console.WriteLine(a);
Console.WriteLine(b);
Console.ReadLine();
}
字符串截取
substring
static void Main(string[] args)
{
string email = "xiaoqiang@qq.com";
string name = email.Substring(0,email.IndexOf("@"));
Console.WriteLine("用户姓名: "+name);
string emailType = email.Substring(email.IndexOf("@")+1);
Console.WriteLine("邮箱类型是:"+emailType);
Console.ReadLine();
}
}
** 字符格式化Format()方法与其类似**
string newstring =string.format("格式字符串",参数列表)
static void Main(string[] args)
{
string name = "xiaohong";
string emailtype = "qq.com";
string info = "姓名是:{0},邮箱是:{1}";
info = string.Format(info,name,emailtype);
Console.WriteLine(info);
Console.ReadLine();
}
字符串为空
1. name.Length==0;
2. name== string.Empty;
3. name=="";
空字符串的使用方法
if(name==""){}
if(name.Equals(string.Empty)){}
- 注意null和”“的区别
去掉前后多余空格
string name =" name ";
name=name.Trim();
转换成大小写
string ToUpper();
string ToLower();
找到最后一个匹配项所在的索引位置
LastIndexOf(string value)
字符串 拼接
string a="我是";
string b="小强";
string c="的弟弟";
//1. 直接+
string c=a+b;
//. stringbuilder.append()