1. 在.net这个平台上开始桌面应用程序,叫做Winform应用程序开发。 2. 解决方案中可以包含多个项目,项目可以包含多个文件,文件中是我们写的程序。 using System; using System.Collections.Generic; using System.Linq; using System.Text; -引用命名空间
namespace Programs {
class Program {
static void Main(string[] args) {
} } }
-定义了一个命名空间,叫:Programs
class Program {
static void Main(string[] args) {
} }
输出变量的值连_赋值符_连接符_占位符 赋值运算符:= a=a+1; 这里的=号是赋值运算符,不是数学意义上的相等。 变量可以重复赋值,一旦给一个变量赋了新值,那么变量中的老值就不复存在了。 右边代码的输出结果: int age = 10; age = 20; Console.WriteLine(age);
“+”在C#中有两个意义 1.是数学中的加法运算:两边参与加法运算的数据类型为数字类型,则“+”表示数学上的加法的意义 2.是连接意义:两边参与加号运算的数据,有一个是字符类型,那么这个“+”就是连接意义 string a = "1"; int b = 2; Console.WriteLine(a+b); 输出的值为12
长数字用string string wname="张三"; int wage=18; char wsex='男'; string phone="010-110-2"; Console.WriteLine("我叫{0},今年{1}岁了,性别{2},电话{3}",wname,wage,wsex,phone); 变量交换 int a = 10; int b = 5; int temp; temp = a; a = b; b = temp; Console.WriteLine("a={0},b={1}",a,b);
算术运算符及算数表达式 算术运算符: + — * / % (去余数 取模) 算数表达式:由算数运算符连接起来的式子。如:1+1 a-b (变量ab前面已经声明并赋初值) 优先级:先乘除,后加减,有括号先算括号里面的,相同级别从左至右运算 小括号可以无限制的套用,但一定要成对出现。 int chinese = 90; int math = 80; int english = 91; int sun = chinese + math + english; int avg = sun / 3; Console.WriteLine("您的总成绩是{0},平局成绩是{1}", sun, avg);
发生自动转换的条件 1.参与运算的两个数据类型必须相兼容 2.必须一边的数据类型表示范围小,一边的范围大,比且把小的转向大的不丢失精度 Double中包含int //int a = 10, b = 3; //int mod = a % 3; 【%取余数,输出1】 //double quo = a / b; 【/取模,输出3,将int转化为double类型】 //Console.WriteLine("mod={0} quo={1}", mod, quo); 一个表达式,如果其中一个操作数为double类型,那么整个数为double类型(double的精度为15) int a = 10, b = 3; int mod = a % 3; double quo = 1.0*a / b; Console.WriteLine("mod={0} quo={1}", mod, quo); 这里quo输出的值为3.333333333333333,为double类型,这是double除以double,如果为int除以double则显示3
int 变量=Consvert.ToInt32(待转字符串) 上面语句的意义:把待转字符串转换成int类型后赋给变量 string strsCore="90"; int iScore = Convert.ToInt32(strScore); 把strScroe转化成int类型并且付给iScroe Console.WriteLine("请输入你的语文成绩?"); string chinese = Console.ReadLine(); Console.WriteLine("请输入你的数学成绩?"); string math = Console.ReadLine(); int chineseScroe = Convert.ToInt32(chinese); int mathScroe =Convert.ToInt32(math);
Console.WriteLine("你的总成绩为:{0}",chineseScroe + mathScroe); 缩写直接赋值 int chinese =Convert.ToInt32( Console.ReadLine());