C#学习入门第二篇

 
4.转义字符
\b    退格符
\n     换行
\r     回车,移到本行开头
\t     水平制表符
\\      代表反斜线字符“\“
\'        代表一个单引号字符
@字在字符串前面表示不转译
 
5.
隐式转换:

double>int

强制转换:
double a = 3.14;
int b = (int)a;
 
 
 
6.//自加自减对自身的加减 直接影响
自增:
a++(运算后再自加)
++a(先加后参与运算)

自减:
a-- (运算后再自减)
--a(先减后参与运算)
 
例子1:
using System;

namespace _014输入秒
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("请输入秒数:");
            string Seconds = Console.ReadLine();
            int ms = Convert.ToInt32(Seconds);
           
            int b = 24 * 60 * 60;
            int day = ms / b;
            int hour = ms % b / 3600;//把一天的秒数减去,然后除以一个小时的秒数
           
            int minute = ms % 3600 /60;
            int d = ms % b % 3600 % 60;
            Console.WriteLine("107653秒是{0}天,{1}小时,{2}分钟,{3}秒",day,hour,minute,d);
        
           
        }
    }
}

 例子2:

例子2:强制转换
输入年龄,输入性别,输入工资
           Console.WriteLine("请输入年龄");
           string age=   Console.ReadLine();
            int ag= Convert.ToInt32(age);
            Console.WriteLine("请输入性别");
            string sex = Console.ReadLine();
            char c=  Convert.ToChar(sex);
            Console.WriteLine("请输入工资");
            string GZ = Console.ReadLine();
           double d= Convert.ToDouble(GZ );
            Console.WriteLine("输入年龄");
            string age= Console.ReadLine();
            int a=  int.Parse(age);
            double.Parse
例子3:判断是否是闰年
using System;

namespace _017
{
    class Program
    {
        static void Main(string[] args)
        {
            //判断是否是闰年
            Console.WriteLine("请输入年份:");
            int year = int.Parse(Console.ReadLine());
            bool a = year % 400 ==0 || year % 4 == 0 && year % 100 != 0;
            Console.WriteLine(a);
        }
    }
}

 例子4:

 
using System;

namespace _023输入密码
{
    class Program
    {
        static void Main(string[] args)
        {
Console.WriteLine("请输入密码:");
            string MM = Console.ReadLine();
            if (MM == "88888")
            {
                Console.WriteLine("正确");
            }
            else
                Console.WriteLine("错误,请从新输入");
                Console.WriteLine("请输入密码:");
                string a = Console.ReadLine();
                if (a == "88888")
                {
                    Console.WriteLine("正确");
                }
                else
                Console.WriteLine("错误");
        }
    }
}

 另一方法:

using System;

namespace _023输入密码
{
    class Program
    {
        static void Main(string[] args)
        {
            int count = 0;
            while (count < 2)
            {

                Console.WriteLine("请输入密码:");
                string MM = Console.ReadLine();
                if (MM == "88888")
                {
                    Console.WriteLine("正确");
                    break;
                }
                else
                    count = count + 1;

                Console.WriteLine("输入有误,请重新输入:");
            }
}
}

例子5:

 
 
using System;

namespace _026密码2
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("请输入用户名:");
            Console.WriteLine("请输入密码:");
            string name = Console.ReadLine();
            string pad = Console.ReadLine();
            if (name == "admin" && pad == "88888")
            {
                Console.WriteLine("正确");
            }
            else if (name != "admin")
            {
                Console.WriteLine("用户名错误");
            }
            else if (pad != "88888")
            {
                Console.WriteLine("密码错误");
            }
            
        }
    }
}

 
 
 

猜你喜欢

转载自www.cnblogs.com/huang--wei/p/9381991.html