C#基础入门练习

1.练习

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _01
{
    class Program
    {
        static void Main(string[] args)
        {
            //有个叫卡卡西的人在旅店登记的时候前台让他填一张表,
            //这张表的里的内容要存到电脑上,
            //有姓名、年龄、邮箱、家庭住址,工资.
            //之后把这些信息显示出来
            //string name = "卡卡西";
            //int age = 30;
            //string email = "kakaxi@qq.com";
            //string address = "火影村";
            //decimal salary = 5000m;

            //Console.WriteLine("我叫{0},我今年{1}岁了,我住在{2},我的邮箱是{3},我的工资是{4}", name, age, address, email, salary);


            //Console.WriteLine("我叫" + name + ",我住在" + address + ",我今年" + age + "岁了" + ",我的邮箱是" + email + ",我的工资是" + salary);
            //Console.ReadKey();


            //int age = 18;
            //age = 81;
            //Console.WriteLine("原来你都" + age + "岁了呀");
            //Console.ReadKey();


            //3.定义四个变量,分别存储一个人的姓名、性别(Gender)、年龄、电话
            //(TelephoneNumber)。然后打印在屏幕上 (我叫X,我今年 X岁了,我是X生,
            //我的电话是XX)(电话号用什么类型,如:010-12345)
            string name = "张三";
            char gender = '男';
            int age = 18;
            string tel = "12345678900";
            Console.WriteLine("我叫{0},我今年{1}岁了,我是{2}生,我的电话是{3}", name, age, gender, tel);
            Console.ReadKey();


        }
    }
}

2.占位符

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _02
{
    class Program
    {
        static void Main(string[] args)
        {
            int n1 = 10;
            int n2 = 20;
            int n3 = 30;
            Console.WriteLine("第一个数字是{1},第二个数字是{0},第三个数字是{2}", n1, n2,n3);
            Console.WriteLine("第一个数字是:" + n1 + ",第二个数字是:" + n2 + ",第三个数字是:" + n3);
            Console.ReadKey();
        }
    }
}

3.变量交换

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _03
{
    class Program
    {
        static void Main(string[] args)
        {
            //int n1 = 10;
            //int n2 = 20;

            //int temp = n1;
            //n1 = n2;
            //n2 = temp;


            //Console.WriteLine("交换后,n1的值是{0},n2的值是{1}", n1, n2);
            //Console.ReadKey();

            //请交换两个int类型的变量,要求:不使用第三方的变量
            int n1 = 50;
            int n2 = 30;
            //n1=20  n2=10;

            n1 = n1 - n2;//n1=-10 n2=20
            n2 = n1 + n2;//n1=-10 n2=10
            n1 = n2 - n1;
            Console.WriteLine("交换后,n1的值是{0},n2的值是{1}", n1, n2);
            Console.ReadKey();
        }
    }
}

4.输入打印

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _04
{
    class Program
    {
        static void Main(string[] args)
        {
            //Console.WriteLine("请输入你的姓名");
            我们还想要接收你输入的姓名
            接收用户在控制台的输入  11  3.14  男  张三
            //string name = Console.ReadLine();

            //Console.WriteLine("您的姓名是{0}", name);
            //Console.ReadKey();

            //1.练习:问用户喜欢吃什么水果(fruits),假如用户输入”苹果”,则显示”哈哈,这么巧,我也喜欢吃苹果”
            //Console.WriteLine("你喜欢吃什么水果?");
            //string fruit = Console.ReadLine();
            //Console.WriteLine("这么巧呀,我也喜欢吃{0}", fruit);
            //Console.ReadKey();

            //2.练习:请用户输入姓名性别年龄,当用户按下某个键子后在屏幕上显示:您好:XX您的年龄是XX是个X生
            //Console.WriteLine("请输入您的姓名");
            //string name = Console.ReadLine();

            //Console.WriteLine("请输入您的年龄");
            //string age = Console.ReadLine();


            //Console.WriteLine("请输入您的性别");
            //string gender = Console.ReadLine();

            //Console.WriteLine("{0}你的年龄是{1}是个{2}生",name,age,gender);
            //Console.ReadKey();

        }
    }
}

5.转义符

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _05
{
    class Program
    {
        static void Main(string[] args)
        {
            //  Console.WriteLine("今天天气好晴\n朗处处好风光");


            //Console.WriteLine("我想在这句话中输出一\"\"个英文半角的双引号");

            //string name1 = "张三";
            //string name2 = "李思思";
            //string name3 = "王小五";
            //string name4 = "李立";
            //Console.WriteLine("{0}\t{1}", name1, name2);                                                            
            //Console.WriteLine("{0}\t{1}", name3, name4);

          

            //string str = "今天天气好晴\r\n朗处处好风光";

            //System.IO.File.WriteAllText(@"C:\Users\SpringRain\Desktop\1111.txt", str);
            //Console.WriteLine("写入成功!!!");
            //Console.ReadKey();


            //char c = '\b';//\ 在里面起到了一个转义的作用
            //char cc='bb';

            //string path=@"F:\生活\music\a\b\c\d\e\学习.doc";
            //Console.WriteLine(path);
            //Console.ReadKey();


            Console.WriteLine(@"今天天气好晴
朗处处好风光");
            Console.ReadKey();

        }
    }
}

6.算数运算

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _06
{
    class Program
    {
        static void Main(string[] args)
        {
            //int n1 = 10;
            //int n2 = 3;
            //int result = n1 / n2;
            Console.WriteLine(result);
            Console.ReadKey();

            显示:某学生三门课成绩为,语文:90  数学:80  英语:67,编程求总分和平均分.
            //int chinese = 90;
            //int math = 80;
            //int english = 67;
            //Console.WriteLine("总成绩是{0},平均成绩是{1}", chinese + math + english, (chinese + math + english) / 3);
            //Console.ReadKey();


            //练习2:计算半径为5的圆的面积和周长并打印出来.(pi为3.14)面积:pi*r*r; Perimeter(周长)

            //=号两遍的数据类型必须一样
            //int r = 5;
            //double area = 3.14 * r * r;
            //double perimeter = 2 * 3.14 * r;
            //Console.WriteLine("圆的面积是{0},周长是{1}", area, perimeter);
            //Console.ReadKey();


            //练习3:某商店T恤(T-shirt)的价格为35元/件,
            //裤子(trousers)的价格为120元/条.小明在该店买了3件T恤和2条裤子,
            //请计算并显示小明应该付多少钱?


            //int T_shirt = 35;
            //int trousers = 120;
            //int totalMoney = 3 * T_shirt + 2 * trousers;
            //Console.WriteLine(totalMoney);

            //double realMoney = totalMoney * 0.88;
            //Console.WriteLine(realMoney);

            //Console.ReadKey();
            打8.8折后呢?

            //int number = 10;
            //int---double
            //double d = number;//自动类型转换 隐式类型转换   int--double

            //double d = 303.6;
            语法:
            (待转换的类型)要转换的值;
            double----int//强制类型转换  显示类型转换
            //int n = (int)d;
            //Console.WriteLine(n);
            //Console.ReadKey();
        }
    }
}

7.类型转换

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _07
{
    class Program
    {
        static void Main(string[] args)
        {
            int n1 = 10;
            int n2 = 3;
            double d = n1*1.0 / n2;
            Console.WriteLine("{0:0.0000}",d);
            Console.ReadKey();


            int n1 = 5;
            //double n1 = 5;
            //int n2 = 2;
            //double d = n1 / n2;
            //Console.WriteLine(d);
            //Console.ReadKey();
        }
    }
}

 

 

 

 

  • 4
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

挑战不可

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值