学习C#-第一章-练习题(流程控制语句)

本文详细介绍了C#编程中关于流程控制语句的练习题,包括条件判断、循环控制、数值操作等多个方面。通过一系列实例,如判断数字特性、分数等级划分、数字反转等,深入理解C#的基础语法和逻辑处理能力。
摘要由CSDN通过智能技术生成

1.已知变量c = 7,d = 12;判断c是否是能被3整除的数;判断d是否是4的倍数\

namespace TEST
{
   
    class Program
    {
   
        static void Main(string[] args)
        {
            int c = 3, d = 12;
            if (c % 3 == 0)
            {
                Console.WriteLine("c能被3整除");
            }
            else
            {
                Console.WriteLine("c不能被3整除");
            }
            if (d % 4 == 0 && d != 0)
            {
                Console.WriteLine("d是4的倍数");
            }
            else
            {
                Console.WriteLine("d不是4的倍数");
            }
        }
    }
}

2.定义一个变量保存一个分数,判断该分数属于什么等级:60分以下E,60~70D,70~80C,80~90B,90或90以上是A

namespace TEST
{
    class Program
    {
        static void Main(string[] args)
        {
            int myScore = Convert.ToInt32(Console.ReadLine());
            if (myScore >= 90)
            {
                Console.WriteLine("A");
            }
            else if (myScore >= 80)
            {
                Console.WriteLine("B");
            }
            else if (myScore >= 70)
            {
                Console.WriteLine("C");
            }
            else if (myScore >= 60)
            {
                Console.WriteLine("D");
            }
            else if (myScore >= 0)
            {
                Console.WriteLine("E");
            }

            Console.ReadKey();
        }
    }
}

3.定义一个变量保存一个不多于五位数的整数,要求:一、求它是几位数,二、逆序打印出各位数字。

namespace TEST
{
    class Program
    {
   
        static void Main(string[] args)
        {
            int num = Convert.ToInt32(Console.ReadLine());
            int a;
            int index = 0;
            bool flag = true;
            int num2 = num;
            while (num > 0)
            {
                num = num / 10;
                index++;
            }
            Console.WriteLine("这个数有{0}位", index);
            while (true)
            {
                int x;
                x = num2 % 10;
                num2 = num2 / 10;
                index--;
                Console.Write(x+" ");
                if (index == 0)
                {
                    break;
                }
            }

            Console.ReadKey();
        }
    }
}

4.定义一个变量保存一个整数,然候逆序输出各个数字,并组成一个全新的数字。比如:”123”则输出”321”,如果是”120”则输出”21”

namespace TEST
{
    class Program
    {
   
        static void Main(string[] args)
        {
            int num = Convert.ToInt32(Console.ReadLine());
            int ys = num;
            int num1 = num;
            int count = 0;
            double a = 0;
            while (num > 0)
            {
                num = num / 10;
                count++;
            }
            //Console.WriteLine(count);
            while (count >= 1)
            {
                ys = num1 % 10;
                num1 /= 10;
                a = ys * Math.Pow(10, count - 1) + a;
                count--;
            }
            Console.WriteLine(a);

            Console.ReadKey();
        }
    }
}

5.输出定义三个变量分别存储双精度数字,进行比较,输出其中最小数。

namespace TEST
{
    class Program
    {
        static void Main(string[] args)
        {
            double num1 = 1.23, num2 = 2.324, num3 = 3.32, min;
            min = num1 < num2 ? num1 : num2;
            min = min < num3 ? min : num3;
            Console.WriteLine(min);

            Console.ReadKey();
        }
    }
}

6.定义三个变量x,y,z分别保存三个整数,请把这三个数由小到大输出。

namespace TEST
{
    class Program
    {
        static void Main(string[] args)
        {
            int num1 = 321, num2 = 2, num3 = 43, min, mid = 0, max;
            if (num1 < num2)
            {
                min = num1;
                max = num2;
                if (min < num3) { }
                else { min = num3; }
                if (max > num3) { }
                else { max = num3; }
            }
            else
            {
                min = num2;
                max = num1;
                if (min < num3) { }
                else { min = num3; }
                if (max > num3) { }
                else { max = num3; }
            }
            Console.WriteLine(max);
            if (min < num1 && num1 < num3)
            {
                mid = num1;
            }
            else if (min < num2 && num2 < max)
            {
                mid = num2;
            }
            else if (min < num3 && num3 < max)
            {
                mid = num3;
            }
            Conso
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值