C#学习笔记之(Hellow,WORLD;常量和变量;键盘输入和输出;数据类型转换;计算器)
HELLO,WORLD |
||||||||||||||
|
||||||||||||||
namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Console.WriteLine("hello,world!"); Console.ReadKey(); } } } |
||||||||||||||
常量和变量 C#中变量的名称规则: 组成:英文字母、数字、下划线_ 开头:英文字母,下划线_ 不能使用关键字 C#中常量的使用: CONST 数据类型=值; C#中常量命名规则: 有意义,均大写,长度不要太长 |
||||||||||||||
namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Console.WriteLine("Hello,World"); const int num1 = 7; int num2 = 3; Console.WriteLine(num1 + num2); Console.WriteLine("***********************"); num2 = 5; Console.WriteLine(num1 + num2); Console.WriteLine(num2); Console.ReadKey(); } } } |
||||||||||||||
键盘输入和2种方式得键盘输出 + 占位符 |
||||||||||||||
namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Console.WriteLine("请输入你的名字:"); string name = Console.ReadLine(); Console.WriteLine("你的名字是:" + name); Console.WriteLine("**********"); Console.WriteLine("你的名字是:{0}***{1}",name,name); Console.ReadKey(); } } } |
||||||||||||||
数据类型和数据类型的转换
|
||||||||||||||
namespace WindowsFormsApplication3 { class Program { static void Main(string[] args) { int num = 5; long num2 = 7; char xx = '1'; double ww = 12.33; int num3 = (int)ww; bool yy = false; string aa = "亚亚"; int[] arr = new int[5] { 0, 1, 2, 3, 4 }; int[] arr1 = new int[] { 0, 1, 2, 3, 4 }; // 省略长度 int[] arr2 = { 0, 1, 2, 3, 4 }; // 省略new string[] arr3 = { "亚亚","沁"}; Console.WriteLine(num); Console.WriteLine(num2); Console.WriteLine(xx); Console.WriteLine(ww); Console.WriteLine(num3); Console.WriteLine(yy); Console.WriteLine(aa); Console.WriteLine(arr[4]); Console.WriteLine(arr1[0]); Console.WriteLine(arr2[3]); Console.WriteLine(arr3[0]); Console.ReadKey(); } } } |
||||||||||||||
计算器 |
||||||||||||||
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace addition { class Program { static void Main(string[] args) { Console.Write("请输入第一个数字:"); double num1 = Convert.ToDouble(Console.ReadLine()); Console.Write("请输入运算符(+、-、*、/):"); string p = Console.ReadLine(); Console.Write("请输入第二个数字:"); double num2 = Convert.ToDouble(Console.ReadLine()); double result=0; switch (p) { case "+": result = num1 + num2; break; case "-": result = num1 - num2; break; case "*": result = num1 * num2; break; case "/": result = num1 / num2; break; } Console.WriteLine(num1 + p + num2 + "的结果为:" + result); Console.ReadKey(); } } } |