c#课本例题第二章

在屏幕上显示Hello, World

using System;//第一行程序通知c#编译器找寻定义在system名称空间(Nemespace)中的一些类和方法
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ex02_01
{
     class Hello  //Hello类(类的名称不一定要和源文件程序文件名称一致)
    {
        static void Main(string[] args)//static:不用事先建立一个对象的实例
        {
            Console.WriteLine("Hello, World!");//console处理屏幕和键盘的I/O动作、writeLine是一个方法,用来输入一行文字到console中显示
            Console.Read();//等待按键
        }
    }
}

注释的用法

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



namespace ex02_02
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("String0"/*字符串1*/
                + "String1");                   //字符串2
            Console.ReadLine();
        }
    }
}

在屏幕上显示9*9乘法表

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

namespace ex02_03
{
     class Program
    {
        static void Main(string[] args)
        {
            for(int i=1;i<=9;i++)
            {
                for (int n = 1; n <= i; n++)
                    Console.Write("{0}*{1}={2,2}\0", i, n, i * n);

                Console.WriteLine();
            }
            Console .ReadLine ();
        }
    }
}

结构类型、枚举类型的定义和使用

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

namespace ex02_04
{
    internal class Program
    {
        enum TMonth { Jan=1,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec};
        struct TDate
        {
            public int Year, Day;
            public TMonth Month;
        }
        static void Main(string[] args)
        {
            TDate BirthDay, Today;
            BirthDay.Year = 1938;
            BirthDay.Month = TMonth.Mar ;
            BirthDay.Day = 4;
            Console.WriteLine("Birthday={0}-{1}-{2}",BirthDay.Year,BirthDay.Month,BirthDay.Day);
            Today = BirthDay;
            Today.Year = 2022;
            Console .WriteLine("Today={0}-{1}-{2}",Today.Year,Today .Month,Today.Day);
            Console.ReadLine(); 
        }
    }
}

数组类型的定义和使用

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

namespace ex02_05
{
    internal class Program
    {
        static void Main(string[] args)
        {
            int[] WeekSales = new int[7];
            int[] StuScore;
            string[] MyFriends;
            WeekSales = new int[7] { 2200, 6600, 3700, 4422, 880, 557, 4500 };
            StuScore = new int[] { 75, 85, 93, 78, 84, 78, 57, 70, 88 };
            MyFriends = new string[] { "Simon", "Mike", "Rose", "Lee" };
            Console.WriteLine("在WeekSales数组中操作");
            Console.WriteLine("周三和周五的销量分别为{0},{1}", WeekSales[2], WeekSales[4]);
            Console.WriteLine("在StuScore数组中操作");
            int Max = StuScore[0];
            int no = 0;
            for (int i = 0; i <= 8; i++)
                if (Max < StuScore[i])
                {
                    no = i; Max = StuScore[i];
                }
            Console.WriteLine("最高成绩是第{0}门,课程成绩:{1}", no, StuScore[no]);
            Console.WriteLine("在MyFriends数组中操作");
            Console.WriteLine("我的第一个好友是:{0}", MyFriends[0]);
            Console.ReadKey();
        }
    }
}

求圆柱体体积

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

namespace ex02_06
{
    internal class Program
    {
        static void Main(string[] args)
        {
            const double pi = 3.1415926;
            double r, h;
            Console.Write("请输入圆柱体半径:");
            r=Convert.ToDouble(Console.ReadLine());
            Console.Write("请输入圆柱体高度:");
            h=Convert.ToDouble(Console.ReadLine());
            double v = pi * r * r * h;
            Console.WriteLine(v);
            Console.ReadLine();

        }
    }
}

用if嵌套实现百分制成绩转成五分制成绩

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

namespace ex02_07
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Console.Write("请输入一百分制成绩:");
            double x=Convert.ToDouble(Console.ReadLine());
            string wfz;
            if (x > 100) wfz = "出错";
            else if (x >= 90) wfz = "A";
            else if (x >= 80) wfz = "B";
            else if (x >= 70) wfz = "C";
            else if (x >= 60) wfz = "D";
            else wfz= "E";
            Console.WriteLine("五分制成绩为:{0}", wfz);
            Console.ReadLine();
        }
    }
}

用switch嵌套实现百分制成绩转成五分制成绩

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

namespace ex02_08
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Console.Write("请输入一百分制成绩:");
            double x = Convert.ToDouble(Console.ReadLine());
            string wfz;
            switch((int)(x/10))
            {
                case 0:case 1:case 2: case 3:case 4:case 5:wfz = "E";break;
                case 6:wfz = "D";break;
                case 7: wfz = "C"; break;
                case 8: wfz = "B"; break;
                case 9:
                case 10: wfz = "A"; break;
                default: wfz = "出错";break;
            }
          
            Console.WriteLine("五分制成绩为:{0}", wfz);
            Console.ReadLine();
        }
    }
}

计算1+2+。。。+n

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

namespace ex02_09
{
    internal class Program
    {
        static void Main(string[] args)
        {
            int s = 0, i = 1;
            Console.Write("请输入一个正整数:");
            int n=Convert.ToInt16(Console.ReadLine());
            while(i<=n)
            {
                s = s + i;
                i++;
            }
            Console.WriteLine(s);
            Console.ReadLine();
        }
    }
}

定义一个姓名列表放入数组中,对该列表的每个人生成会议通知。

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

namespace ex02_10
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string[] strNames = { "张三", "李四", "王五", "赵六", "陈七" };
            List<string>mylist=new List<string> (strNames);
            foreach(string str in mylist)
                Console.WriteLine("尊敬的{0}:你好!请你明天上午10:00准时参加教学研讨会,谢谢!",str);
            Console.ReadLine();

        }
    }
}

输入一个整数max,计算1+2+。。。+100的和,求和小于max情况下的最大n值及和

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

namespace ex02_11
{
    internal class Program
    {
        static void Main(string[] args)
        {
            int s = 0, n = 0;
            Console.Write("请输入一个正整数:");
            int max=Convert.ToInt16(Console.ReadLine());
            while(n<100)
            {
                if (s + n + 1 >= max) break;
                n++;
                s = s + n;
            }
            Console.WriteLine("n={0},s={1}", n, s);
            Console.ReadLine();
        }
    }
}

计算1+2+4+5+7+。。。+n的和

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

namespace ex02_12
{
    internal class Program
    {
        static void Main(string[] args)
        {
            int s = 0, i = 0;
            Console.Write("请输入一个正整数:");
            int n=Convert.ToInt16(Console.ReadLine());
            while (i <= n)
            { 
                if (i % 3 == 0) { i++;continue; }
                s = s + i;
                i++;
            }
            Console.WriteLine(s);
            Console.ReadLine();
        }
    }
}

计算一个数的平方根

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

namespace ex02_13
{
    internal class Program
    {
        static void Main(string[] args)
        {
        begin:
            Console.Write("请输入一个正数:");
            double x=   Convert.ToDouble(Console.ReadLine());
            if(x<0)
            {
                Console.WriteLine("输入的数必须不小于0");
                goto begin;
            }
            else
                Console.WriteLine("该数的平方根为:{0}",Math.Sqrt(x));
            Console.ReadLine();
        }
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值