C#练习代码
程序员文章站
2024-01-17 14:36:40
...
using System; class Program { static void Main() { Console.WriteLine("请输入两个整数:"); int a = Convert.ToInt32(Console.ReadLine());//Console.ReadLine()默认读取字符串 int b = Convert.ToInt32(Console.ReadLine());//Console.ToInt32()将读到的字符转换为带符号的32位整数 int c = a + b; Console.WriteLine("两数之和是:" + c); Console.ReadLine(); } } using System; class Program { static void Main() { short a = 32767; a = a + (short)1; Console.WriteLine(a); } } using System; class Program { static void Main() { int o = sizeof(byte); int a = sizeof(char); int b = sizeof(short); int c = sizeof(int); int d = sizeof(long); Console.WriteLine("在32位环境中字节型数所占的字节数为:" + o); Console.WriteLine("在32位环境中字节型数所占的字节数为:" + a); Console.WriteLine("在32位环境中字节型数所占的字节数为:" + b); Console.WriteLine("在32位环境中字节型数所占的字节数为:" + c); Console.WriteLine("在32位环境中字节型数所占的字节数为:" + d); } } Convert.ToInt32("213145");//用Convert类将一个.NET基本数据类型转换成另一个.NET基本数据类型 int a=12345; string sn=a.ToString();//把a转换成字符串sn int b=int.Parse(sn);//把sn转换成整数b,结果b=a //数据发生阶段示例 using System; class Program { static void Main() { short i = 289; byte b; b = (byte)i; char c = (char)b; Console.WriteLine("{0}", c); } } using System; class Program { static void Main() { int a = 12; a -= a * a; a += a; Console.WriteLine("a={0}", a); } } using System; class Program { static void Main() { bool a = true; bool b = 5 < 3 && 7 > 4 || 8 > 5; int c = Convert.ToInt32(a && b); Console.WriteLine("c={0}", c); } }//1 using System; class Program{ static void Main(){ int a=10,b=20,c=30; bool m=(a<b&&b>c||a<b); Console.WriteLine(m); } }//true using System; class Program { static void Main() { Console.Write("请输入第一个整数:"); int a = Convert.ToInt32(Console.ReadLine()); Console.Write("请输入第二个整数:"); int b = Convert.ToInt32(Console.ReadLine()); int max = a > b ? a : b; Console.WriteLine("两个整数的较大数是:{0}", max); } } using System; class Program { static void Main() { bool b1 = false; Console.Write("请输入第一个数:"); int m = Convert.ToInt32(Console.ReadLine()); Console.Write("请输入第二个数:"); int n = Convert.ToInt32(Console.ReadLine()); b1 = m >= n ? true : false; Console.WriteLine("结果为:" + b1); } } using System; class Program { static void Main() { Console.Write("请输入一个整数:"); int x = Convert.ToInt32(Console.ReadLine()); bool m = x > 0 && x <= 100 ? true : false; Console.WriteLine("{0}", m); } } template <class T, class T1 = int, class T2 = int> class A; template<class T, class T1 , class T2 > class A { }; class B: public A<B, int> { }; int main(int argc, char* argv[]) { B b; return 0; } using System; class Program { static void Main() { int a = 240; int b = -16; int c = 44; Console.WriteLine("a={0}右移二位 a={1}", a, a >> 2); Console.WriteLine("b={0}右移二位 b={1}", b, b >> 2); Console.WriteLine("c={0}左移二位 c={1}", c, c << 2); } } using System; class Program { static void Main() { int a = 7, b = 2; int c = a % b++; int d = -b; int e = b / (3 + a); int f = d + e - c; Console.WriteLine("f={0}", f); } } //十进制,十六进制 using System; class Program { static void Main() { int number = 1001; Console.WriteLine("十进制:{0:1}", number); Console.WriteLine("十六进制:{0:X}", number); Console.Write("{0,5}", 25);//三个空格 Console.Write("{0:D5}", 25);// } } //talkback.c--一个能为你提供一些信息的对话框 #include<stdio.h> #include<string.h> #define DENSITY 62.4 int main() { float weight, volume; int size, letters, m; char name[40]; printf("Hi! What's your first name?\n"); scanf("%s",name); printf("%s, what'syour weight in pounds?\n",name); scanf("%f",&weight); size = sizeof name; letters = strlen (name); volume = weight / DENSITY; printf("well, %s, your volume is %2.2f cubic feet.\n",name, volume); printf("alsso, your first name has %d letters, \n",letters); printf("and we have %d bytes to store it in.\n",size); getchar(); return 0; } using System; class Program { static void Main() { Console.Write("请输入一个年份:"); string str = Console.ReadLine(); int year = Int32.Parse(str); bool isleapyear = ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0)); string yesno = isleapyear ? "是" : "不是"; Console.WriteLine("{0}年{1}闰年", year, yesno); } } using System; class Program { static void Main() { Console.Write("请输入一个年份:"); string str = Console.ReadLine(); int year = Int32.Parse(str); if ((year % 4 == 0) && (year % 100 != 0) || (year % 400) == 0) Console.WriteLine("{0}年时闰年", year); else { Console.WriteLine("{0}年不是闰年", year); } } } using System; class Program { static void Main() { Console.Write("请输入一个整数分数:"); int x = Convert.ToInt32(Console.ReadLine()); if (x > 90) Console.WriteLine("优秀"); else if (x > 80) Console.WriteLine("良好"); else if (x > 70) Console.WriteLine("中等"); else if (x > 60) Console.WriteLine("及格"); else Console.WriteLine("不及格"); } } using System; class Program { static void Main() { Console.Write("请输入一个分数:"); string x = Console.ReadLine(); int m = Int32.Parse(x); int c = m / 10; switch(c){ case 9: case 10:Console.WriteLine("优秀");break; case 8:Console.WriteLine("良好");break; case 7:Console.WriteLine("一般");break; case 6:Console.WriteLine("较差");break; default:Console.WriteLine("差");break; } } } using System; class Program { static void Main() { int i = 1, sum = 0; while(i<=100) { sum += 1; i++; } Console.WriteLine("sum={0}", sum); } } using System; class Program { static void Main() { int digit; Console.Write("输入一个整数"); string x = Console.ReadLine(); int num = int.Parse(x); Console.Write("反向显示结果"); while(num!=0) { digit = num % 10; num = num /10; Console.Write(digit); } Console.WriteLine(); } }
上一篇: python jinja2模板的使用示例