C#基础:helloWord book 实例小集合
数据转换,实现double转换成整型,浮点型,字符串型
/// <summary>
/// 数据转换,实现double转换成整型,浮点型,字符串型
/// </summary>
class Program
{
static void Main(string[] args)
{
double mydouble = 87.45;//原始数据
int myint;
float myfloat;
string myString;
myint = Convert.ToInt32(mydouble);
myfloat = Convert.ToSingle(mydouble);
myString = Convert.ToString(mydouble);
string record = string.Format("转换后的整型数为{0}\t转换后的浮点型数为{1}\t转换后字符串型数为{2}", myint, myfloat, myString);
Console.WriteLine(record);
Console.ReadLine();
}
}
实现一个1-100偶数相加
static void Main(string[] args)
{
int result = 0;
for (int i = 1; i <= 100; i++)
{
if (i % 2 == 0)
result += i;
}
Console.WriteLine(result);
Console.ReadLine();
}
实现一个个反转后的数的输出
注:当输入的数的末位有0,如何实现反转
static void Main(string[] args)
{
int Number = 9876;
int Result = 0;
int Thoudous, Hunder, Ten, Single;
Thoudous = Number / 1000;
Number = Number %1000;
Hunder = Number / 100;
Number = Number % 100;
Ten = Number / 10;
Number = Number % 10;
Single = Number;
Result = Single * 1000 + Ten * 100 + Hunder * 10 + Thoudous;
Console.WriteLine("9876的反转数为{0}", Result);
Console.ReadLine();
}
循环打印一个简单的三角形
static void Main(String[] args)
{
//控制行数
int n=1;
while(n<=5)
{
//控制每行输出的*数
int m=1;
while(m<=n)
{
Console.Write("*");
m++;
}
}
n++;
Console.WriteLine();
}
10分钟倒计时
static void Main(String[] args)
{
for(int i=10;i<=0;i--)
{
for(int j=60;j<=0;j--)
{
Console.WriteLine("十分钟倒计,还是剩下的时间为:{0}分,{1}秒",i,j);
Console.ReadLine();
}
}
}
猜人名
static void Main(String[] args)
{
string name="silly";
Console.WriteLine("",);
for(int i=1;i<=3;i++)
{
string guessName=Console.ReadLine();
if(guessName==name)
break;
else
{
Console.WriteLine("猜测错误,你还有{0}次机会",3-i);
}
}
Console.WriteLine("恭喜你,猜对了!");
}