hq金水

愿你是阳光,明媚不忧伤~
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

知识点一主函数、数据类型、类型转换

Posted on 2016-04-11 22:16  hq金水  阅读(226)  评论(0编辑  收藏  举报

一、主函数的内容

输出,输入

输出:
Console.WriteLine();
Console.Write();
1.输出数字
Console.WriteLine(22); //22
Console.WriteLine(12+44); //56
2.输出文字
Console.WriteLine("abc");//abc
Console.WriteLine("78"+"56");//7856
3.整合输出
Console.WriteLine("78+56=" + (78 + 56));//78+56=134   ①

Console.Write("78+56="); 
Console.WriteLine(78 + 56); ②

①的效果=②的效果

输入:
string aaa = Console.ReadLine();

整合应用:
//输入
string s = Console.ReadLine();   //输入Console.ReadLine()默认字符串型
//输出
Console.WriteLine("你刚才输入的是:"+s);


案例1:
做一个加法器:
显示:请输入一个数。
等待输入
显示:请输入另一个数
等待输入
显示最终的运算结果。

namespace ConsoleApplication2
{
class Program

{

static void Main(string[] args)
{ Console.WriteLine("输入一个数");                                                                   
string a=Console.ReadLine();
Console.WriteLine("另输入一个数");
string b=Console.ReadLine();
double c =Convert.ToDouble(a)+Convert.ToDouble(b);
Console.WriteLine("{0}+{1}={2}",a,b,c);
}

 

}

}

 

效果如下:

输入一个数

5

另输入一个数

6

5+6=11

 

案例2:

static void Main(string[] args)
{
Console.WriteLine("********信息调查表*********"); //显示标题
Console.Write("姓名:");
string xm = Console.ReadLine();
Console.Write("性别:");
string xb = Console.ReadLine();
Console.Write("毕业学校:");
string byxx = Console.ReadLine();
Console.Write("所学专业:");
string sxzy = Console.ReadLine();

//显示:
Console.WriteLine("*********刚才输入的内容如下************");
Console.WriteLine("姓名:"+xm+" 性别:"+xb);
Console.WriteLine("毕业学校:"+byxx);
Console.WriteLine("所学专业:"+sxzy);
}

 

案例3:

static void Main(string[] args)
{
Console.WriteLine("小hi:您叫什么名子?");
Console.Write("我:");
string xm = Console.ReadLine();
Console.WriteLine("小hi:哦,原来你就是"+xm+"啊,久仰了!,你喜欢什么好吃的?");
Console.Write("我:");
string sw = Console.ReadLine();
Console.WriteLine("小hi:我也喜欢吃"+sw+",你能吃多少啊?");
Console.Write("我:");
string sl = Console.ReadLine();
Console.WriteLine("小hi:你居然吃"+sl+",比我吃得多多啦");


}

二、数据类型:
1、基本类型
整型 int 只能放有限位数的整数-xxxx ~ xxxx,比电话号码少一位。
   long
   short

浮点 double 默认小数都是double
  float 如果小数要作为float看待,需要在后面加上f,F的后缀'
  float a = 3.14f; float a = 3.14;//错

字符 char 用单引号,只能放一个字符。转义字符除外
char c = 'A'; char c = "A";//错 char c='ABC';//错

布尔 bool 只能放true,false。代码中只能是小写,而且不要加引号。
bool g = true; bool g = True;//错 bool g = "true";//错

2、引用类型
字符串 string 双引号,一个或多个字符组成。
string s = "abcdef"; string s = "a"; string s = 'asdfasdf';//错

 

二、类型转换:
1.自动转换 - 只要不可能存在数据丢失的情况,都会自动转。
2.强制转换
对于数字:在被转换的值的左边加上小括号,在小括号中写上被转成哪种类型。
float a = (float)3.14;
对于字符串:字符串转成相对应的基本类型
法一:int a = int.Parse("字符串"); float b = float.Parse("字符串"); double c = double.Parse("字符串");bool d = bool.Parse("字符串")
法二:使用Convert
double d = Convert.ToDouble(s); float f = Convert.ToSingle(s);
int a = Convert.ToInt32(s);

 

案例1:

static void Main(string[] args)
{

//string s = "4.5";
//double d = double.Parse(s);
//double d = Convert.ToDouble(s);

//Console.WriteLine(d+1);

//string s = "4.5";
//double d = double.Parse(s);
//int a = (int)d;
//Console.WriteLine(a);

//int a = 10;
//float b = (float)9.8;
//double c = a + b;

//a = (int)c;

//Console.WriteLine(a);

//int a = 4;
//long b = 19;

//b = a;

//double c = 3.14;

//float f = 2.22f;

//c = f;

//char d = '\a';
//Console.WriteLine(d);

//bool g = true;
//g = false;
//Console.WriteLine(g);

}

 

案例2:

static void Main(string[] args)
{

string s = "true";
bool b = Convert.ToBoolean(s);

b = !b;

Console.WriteLine(b);

}

 

案例3:


static void Main(string[] args)
{
Console.Write("您今年几岁了?");
string s = Console.ReadLine();
double age1 = Convert.ToDouble(s);
int age2 = (int)age1;
Console.WriteLine("您今{0}岁,明年就{1}岁", age2 + 1,age2);
}

 

案例4:

static void Main00(string[] args)
{

//string s = "c:\\text\\new\\aaa.txt";
//Console.WriteLine(s);
string s = "注意:\"小狗\"是用来表示亲昵的称乎";
Console.WriteLine(s);

//char c = 'A'; //ASCII码
//int b = (int)c;
//Console.WriteLine(b);

//int a = 43;
//char c = (char)a;

//Console.WriteLine(c);

 

//for (int i = 0; i < 128; i++)
//{
// char c = (char)i;
// Console.Write(i+"="+c+"\t");
//}
}

 

备注:

C#转义字符:

\'  单引号

\"  双引号

\\    \

\0    空

\a   警告

\n   换行

\r   回车

\t   水平制表符

ASCII码  

A-65

a-97 

     初学C#语言,就一个感觉,程序员真不简单。