新手 进来学学c#哦 都是实例 (熟练的人员可以不进哦)
首先声明 我也是一个学习c#不过1个多月的新手,最近有个计划 :每天写几个c#实例。知识点都是简单化的,很适合刚开始学习的人,后面会陆续更新其他知识点。目的:大家一起学习。
实例一:
namespace 实例1_hello_world
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");//输出hello world!
Console.ReadKey();//等待窗口的下一个指示或者功能键(例如关闭窗口)
}
}
}
namespace 实例_2
{
class Program
{
static void Main(string[] args)
{
string strName;
Console.Write("input your name:");
strName=Console.ReadLine();
Console.WriteLine("Hello,{0}",strName);
Console.ReadKey();
//write和writeline输出的区别:前面是接着后面继续输入
//后者是先显示括号内的内容,然后换行再输入你要输入的数据
}
}
}
namespace 实例3_命令行参数
{
class Program
{
static void Main(string[] args)
{
string strName;//声明string变量
strName=args[0];//第一个参数赋给变量
Console.WriteLine("{0}",strName);//输出第一个参数
Console.ReadKey();
}
}
}
namespace 实例4_预定义类型变量
{
class Program
{
static void Main(string[] args)
{
string str = "this is a string";//声明string变量
Console.WriteLine(str);//输出str
string strCopy = string.Copy(str);//把str值赋给strcopy
Console.WriteLine(strCopy);
bool testbool =(str==strCopy);//判断是否相等
Console.WriteLine(testbool);
testbool =((object)str==(object)strCopy);//判断两者指的对象是否相等
//float testfloat =2323.03; 这样写是错误的写法,c#中默认的数值类型为double
float testfloat = 2323.03F;
Console.WriteLine(testfloat);
double testdouble = 2323.03;//声明double变量
//double testdouble=2323.03D;这样赋值更加清晰,
Console.WriteLine(testdouble);
testbool = (testfloat == testdouble);
Console.WriteLine(testbool);//false 因为数据类型不同
Console.ReadKey();
}
}
}
namespace 实例5_预定义类型转换
{
class Program
{
static void Main(string[] args)
{
//转换成功的例子
int intValue1, intValue2;
long longValue1, longValue2;
intValue1 = 123;
longValue1 = 456;
longValue2 = intValue1;//隐式转换
intValue2 = (Int32)longValue1;//显示转换
Console.WriteLine("(long){0}={1}",intValue1,longValue2);
Console.WriteLine("(int){0}={1}",longValue1,intValue2);
//转换失败的例子
long longValue3 = 2147483657L;
int intValue3 = (int)longValue3;//int存放的最大数为2147483647, 有溢出
Console.WriteLine("(int){0}={1}",longValue3,intValue3);
Console.ReadKey();
}
}
}
namespace 实例6_if语句
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("put in a char:");
char chLetter=Convert.ToChar(Console.ReadLine()); //把输入的字符转换成char型 输入的数据默认为string
if (chLetter >= 'A'&&chLetter <= 'Z')
Console.WriteLine("{0}是个大写字母", chLetter);
else
{
if (chLetter >= 'a' && chLetter <= 'z')
Console.WriteLine("{0}是个小写字母", chLetter);
else
{
if (Char.IsDigit(chLetter))
Console.WriteLine("{0}是个数字", chLetter);
else
Console.WriteLine("{0}是个特殊字符",chLetter);
}
}
Console.ReadKey();
}
}
}
namespace 实例7_switch_语句
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("三种选择型号:1=(小杯,¥3.0)2=(中杯,¥4.0)3=(大杯,¥5.0)");
Console.Write("您选择的是:");
//读入用户选择
string s = Console.ReadLine();
int n = int.Parse(s);//类型转换 string转int
switch (n)
{
case 1:
Console.WriteLine("小杯,请付费¥3.0.");
break;
case 2:
Console.WriteLine("中杯,请付费¥4.0.");
break;
case 3:
Console.WriteLine("大杯,请付费¥5.0.");
break;
default:
Console.WriteLine("中杯,请付费¥4.0.");
break;
}
//显示结束使用提示
Console.WriteLine("谢谢使用,欢迎再次光临");
//上面的语句其实可以做个简单的控制台程序(*^__^*) 嘻嘻……
//自己动手哦
Console.ReadKey();
}
}
}
,希望楼主能坚持下去
[解决办法]