基础部分:
一、基本操作
Console.WriteLine("Hello world!");
Console.ReadLine();
Console.WriteLine("。。。")相当于输出一行内容,而这里的Console.ReadLIine()的作用为,将程序暂停下来(如果没有,程序将一闪而过)。
输入输出字符串并阅读的方法为:
using System;
namespace ConsoleApp3
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("你的名字是:");
string name = Console.ReadLine();
Console.WriteLine("你的,名字是"+name);
Console.ReadLine();
}
}
}
其中Console为类,而类中包含有工具和属性:WriteLine/ReadLine为工具类型,而Title(改变程序名称)为属性类型 。类.方法->调用语句,使用某个类中的功能。
using system:引入命名空间。
namespace :定义命名空间(类的地址)
clas program:定义类(做工具)
main:函数入口处。
二、变量
float:1.2f
double:1.2d
decimal:1.2m,精度为28-29位,其他浮点小数运算使可能出现误差,此类型精确度更高。
bool:一字节,可直接赋值ture或false,或者表达式做判断。
float a = 1.0f;
float b = 0.9f;
bool c = a-b==0.1f;
Console.WriteLine(c);
Console.ReadLine();
该代码段输出为FALSE,因为浮点数计算准确度不高。
多段变量输出
using System;
namespace ConsoleApp3
{
internal class Program
{
static void Main(string[] args)
{
string name = "毛毛";
string nickname = "歌姬";
string str = String.Format("{0},你就是{1}吧!",name,nickname);
Console.WriteLine(str);
Console.ReadLine();
}
}
}