您好,欢迎访问三七文档
inta=7,b=2;intsum=a+b,dif=a-b,pro=a*b,quo=a/b;Console.WriteLine(sum={0},dif={1},pro={2},quo={3},sum,dif,pro,quo);//功能:求变量inta=7;intb=2;intsum,dif,pro,quo;sum=a+b;dif=a-b;pro=a*b;quo=a/b;Console.WriteLine({0}+{1}={2},a,b,sum);Console.WriteLine({0}-{1}={2},a,b,dif);Console.WriteLine({0}*{1}={2},a,b,pro);Console.WriteLine({0}/{1}={2},a,b,quo);inti=1;while(i=10){if(i==6)break;Console.WriteLine({0},i++);}inti;for(i=1;i=10;i++){if((i%2==0))continue;Console.WriteLine(i);}观察程序,体验加引号和不加引号的区别intbalance=100000;Console.WriteLine(balance);Console.WriteLine(balance);Console.WriteLine('a');Console.WriteLine(a);Console.WriteLine('6');Console.WriteLine(Iloveyou!);字符型变量用来存储一个字符,用关键字char声明charmys;mys='x';//这里是加单引号Console.WriteLine(mys);mys='2';Console.WriteLine(mys);mys='#';Console.WriteLine(mys);stringmys;mys=Howdoyoudo?;Console.WriteLine(mys);mys=Iamfine.;Console.WriteLine(mys);字符串的读取Console.ReadLine()用来读取一条字符串。Console.WriteLine(请输入你的姓名:);stringname=Console.ReadLine();Console.WriteLine(你的名字是:+name);字符的读取Console.Read()用来读取一个字符。Console.WriteLine(请输入一个字符:);intch=Console.Read();//输入一个字符,返回的是一个整型的ASCII编码。Console.WriteLine(ASCII编码为:+ch);数字的读取根据圆的半径求面积doubler,s;Console.Write(r=);r=Convert.ToDouble(Console.ReadLine());//Convert.ToDouble()或Toint32;s=3.14*r*r;Console.WriteLine(s=+s);把一个字符转化为整数,从而得到字符ASCII码intcode=(int)'A';//转化方法:在字符前加(int)Console.WriteLine(A-{0},code);charch=(char)65;//转化方法:在整数前加(char)Console.WriteLine(65-{0},ch);用十六进制形式输出。Console.WriteLine(\u0047\u006f\u006f\u0064\u0021);Console.WriteLine(\u5c0f\u56ed\u9999\u5f84\u72ec\u5f98\u5f8a);chartt='玉';intcode10=(int)tt;stringcode16=Convert.ToString(code10,16);Console.WriteLine({0}的Unicode编码为:{1},tt,code16);Console.WriteLine(What'syourname?\nMynameisJack.);Console.WriteLine(c\\windows\\fonts);Console.WriteLine(小明说:\我和他是朋友。\);Console.WriteLine(@c\windows\fonts);stringmySentence=Harvardis;mySentence=mySentence+cccc;Console.WriteLine(mySentence);floatpi=3.14f;Console.WriteLine(圆周率是+pi);intnumber=100;Console.WriteLine(Thenumberis+number);Console.WriteLine(Thenumberis{0},number);inti=360;intj=60;Console.WriteLine({0}+{1}={2},i,j,i+j);Console.WriteLine({0,6}\n+{1,5}\n------\n{2,6}\n,i,j,i+j);decimali1=19.23m;decimalj1=73.7m;Console.WriteLine({0,8:c2}\n+{1,7:c2}\n------\n{2,8:c2},i1,j1,i1+j1);占位符doublepi=3.1415926;Console.WriteLine({0:#.00},pi);Console.WriteLine({0:#.000},pi);Console.WriteLine({0:#.0000},pi);stringname=张三;intage=28;decimalsalary=7600.23m;Console.WriteLine(我叫{0},今年{1}岁了,我的工资是{2}元。,name,age,salary);Console.WriteLine(请输入你的语文成绩?);intchinese=Convert.ToInt32(Console.ReadLine());Console.WriteLine(请输入你的数学成绩?);intmath=Convert.ToInt32(Console.ReadLine());Console.WriteLine(你的总成绩为:{0},chinese+math);inta=7,b=2;intsum=a+b,dif=a-b,pro=a*b,quo=a/b;Console.WriteLine({0}+{1}={2},a,b,sum);Console.WriteLine({0}-{1}={2},a,b,dif);Console.WriteLine({0}*{1}={2},a,b,pro);Console.WriteLine({0}/{1}={2},a,b,quo);stringname=张三;intage=23;Console.WriteLine({0},我叫+name,今年+age+岁了);intage=18;intsum=age++-10;//先减,等于8Console.WriteLine(summ=+sum);intage=18;intsum=++age-10;//先1+age,再减10,等于9Console.WriteLine(sum=+sum);intvar1,var2=5,var3=6;var1=var2++*--var3;Console.WriteLine(var1);intvar1,var2=5,var3=6;var1=++var2*var3--;Console.WriteLine(var1);intnumber;number=10;Console.WriteLine(\nmynumber={0},number);number=number+1;//在程序中,可以给一个变量多次赋值。变量的当前值等于最后一次给该变量所赋的值。Console.WriteLine(\nmynumber={0}now,number);inta=0;intb=newint();//调用缺省构造子初始化变量Console.WriteLine(value:{0},{1},a,b);Console.Write(请输入第一个学员的姓名:);stringname1=Console.ReadLine();Console.Write(请输入第一个学员的年龄:);stringage1=Console.ReadLine();Console.WriteLine(第一个学员的姓名是+name1+,年龄是+age1+岁。);Console.WriteLine(请输入第二个学员的姓名:);stringname2=Console.ReadLine();Console.WriteLine(请输入第二个学员的年龄:);stringage2=Console.ReadLine();Console.WriteLine(第二个学员的姓名是{0},年龄是{1}岁。,name2,age2);intnum1;intnum2;Console.WriteLine(请输入两个整数:);num1=int.Parse(Console.ReadLine());num2=int.Parse(Console.ReadLine());Console.WriteLine({0}+{1}={2},num1,num2,num1+num2);inta,b,c;//求两数之和a=100;b=50;c=a+b;Console.WriteLine(c=+c);inta=50;intb=40;Console.WriteLine(a+b);stringi=4;stringj=10;Console.WriteLine(i+j);stringname=张三;stringsex=男;intage=23;doubleheight=1.75;Console.Write(我叫:);Console.WriteLine(name);Console.Write(性别:);Console.WriteLine(sex);Console.Write(年龄:);Console.WriteLine(age+岁);Console.Write(身高:);Console.WriteLine(height+米);Console.WriteLine(10+10);Console.WriteLine(10+10);inta=4;a-=a+=a*a;Console.WriteLine(thevalueofais{0},a);intx=10;inty=0;if(x!=0&&y==y/x){Console.WriteLine(Hello!);}doubled=100.5;inti;i=(int)d;//显示转换Console.WriteLine(i);boolmyBool=true;doublemyDouble=99.00;stringmyString=Convert.ToString(myBool)+Convert.ToString(myDouble);Console.WriteLine(theValueofmyStringis{0},myString);int[]array=newint[5];//定义一个一维数组,它有5个元素for(inti=0;iarray.Length;i++)//循环计算i的值,最大长度是数组的长度array.Length{array[i]=i+10;Console.WriteLine(array[i]);//输出i的值}Console.ReadLine();//获取输入焦点,为了看运行结果,放在循环体之外//定义一个二维数组int[,]intArray=newint[2,3];//为二维数组赋值并输出for(inti=0;iintArray.GetLength(0);i++){for(intj=0;jintArray
本文标题:C#基础练习实例
链接地址:https://www.777doc.com/doc-4692868 .html