流程控制语句(2018/6/20)

流程控制语句


选择语句:if
分支语句:switch
if语句有三种形式:

  1. if(条件表达式/布尔值){ 条件为真时执行 }
  2. if(){ }else{ }
  3. if(){}else if(){}else if(){}
switch(变量){
    case 值1:
        ...
        break;
    case 值2:
        ...
        break;
    default:
        ...
        break;
}

练习题


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

using System;
using System.Collections.Generic;

namespace cchoopDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            //定义一个变量保存一个分数,判断该分数属于什么等级:
            //60分以下E,60~70D,70~80C,80~90B,90或90以上是A
            float score = 65.5f;
            Console.Write("分数{0},等级:", score);
            if (score >= 90)
            {
                Console.WriteLine("A");
            }
            else if (score >= 80)
            {
                Console.WriteLine("B");
            }
            else if (score >= 70)
            {
                Console.WriteLine("C");
            }
            else if (score >= 60)
            {
                Console.WriteLine("D");
            }
            else
            {
                Console.WriteLine("E");
            }

            Console.ReadKey();
        }
    }
}

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

using System;
using System.Collections.Generic;

namespace cchoopDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            //定义一个变量保存一个不多于五位数的整数,要求:一、求它是几位数,二、逆序打印出各位数字。
            int number = 803456;   //不多于五位数的整数
            int count = 1;      //计算是几位数
            Console.WriteLine(number + ",逆序打印为:");
            while (number >= 10)
            {
                count++;
                Console.Write(number % 10 + " ");
                number /= 10;
            }
            Console.WriteLine(number);
            Console.WriteLine("位数为:" + count);

            Console.ReadKey();
        }
    }
}

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

using System;

namespace cchoopDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            //定义三个变量分别存储双精度数字,进行比较,输出其中最小数。
            double d1 = 12.5, d2 = 3.2, d3 = 15.2;
            double min = d1;
            if (min > d2)
            {
                min = d2;
            }
            if (min > d3)
            {
                min = d3;
            }
            Console.WriteLine("{0},{1},{2}中最小值为:{3}", d1, d2, d3, min);

            Console.ReadKey();
        }
    }
}

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

using System;

namespace cchoopDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            //定义三个变量x,y,z分别保存三个整数,请把这三个数由小到大输出。
            int x = 15, y = 59, z = 2;
            if (x < y)
            {
                if (y < z)
                {
                    Console.WriteLine("{0}<{1}<{2}",x,y,z);
                }
                else   // x<y z<y
                {
                    if (x < z)
                    {
                        Console.WriteLine("{0}<{1}<{2}", x, z, y);
                    }
                    else  //x>z
                    {
                        Console.WriteLine("{0}<{1}<{2}", z, x, y);
                    }
                }
            }
            else  //y<x
            {
                if (x < z)
                {
                    Console.WriteLine("{0}<{1}<{2}", x, y, z);
                }
                else // y<x z<x
                {
                    if (y < z)  
                    {
                        Console.WriteLine("{0}<{1}<{2}", y, z, x);
                    }
                    else
                    {
                        Console.WriteLine("{0}<{1}<{2}", z, y, x);
                    }
                }
            }

            Console.ReadKey();
        }
    }
}

5.定义一个变量存储一个数字,判断该数字是否是水仙花数
(正三位数,百位立方 + 十位立方 + 个位的立方刚好等于刚数字);

using System;

namespace cchoopDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            // 例如:153 = 1 * 1 * 1 + 5 * 5 * 5 + 3 * 3 * 3;
            int number = 153;
            bool isSXH = false;  //标记number是否是水仙花数

            if (number < 100 || number > 999)
            {
                isSXH = false;
            }
            else
            {
                int ge = number % 10;
                int shi = number / 10 % 10;
                int bai = number / 100;

                if (number == ge * ge * ge + shi * shi * shi + bai * bai * bai)
                {
                    isSXH = true;
                }
                else
                {
                    isSXH = false;
                }
            }

            if (isSXH)
            {
                Console.WriteLine("{0}是水仙花数", number);
            }
            else
            {
                Console.WriteLine("{0}不是水仙花数", number);
            }

            Console.ReadKey();
        }
    }
}

5.定义一个变量存储一个数字,判断该数字是否是回文数;
(个位 = 万位,十位 = 千位,五位数,例如:15351、121、1221)

using System;

namespace cchoopDemo
{
    class Program
    {
        static void Main(string[] args)
        {

            int number = 12221;
            int temp = number;
            int result = 0;

            while (temp > 0)
            {
                result = result * 10 + temp % 10;
                temp /= 10;
            }

            if(result == number)
            {
                Console.WriteLine("{0}是回文数", number);
            }
            else
            {
                Console.WriteLine("{0}不是回文数", number);
            }


            Console.ReadKey();
        }
    }
}

6.企业发放的奖金根据利润提成。
利润(I)低于或等于10万元时,奖金可提10 %;
利润高于10万元,低于20万元时,低于10万元的部分按10 % 提成,高于10万元的部分,可可提成7.5 %;
20万到40万之间时,高于20万元的部分,可提成5 %;
40万到60万之间时高于40万元的部分,可提成3 %;
60万到100万之间时,高于60万元的部分,可提成1.5 %,高于100万元时,
超过100万元的部分按1 % 提成,
定义一个变量存储该月利润,求应发放奖金总数?

using System;

namespace cchoopDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            float profit = 2000000;
            float bonus = 0;

            if (profit < 100000)
            {
                bonus = profit * 0.1f;
            }
            else if (profit < 200000)
            {
                bonus = 100000 * 0.1f + (profit - 100000) * 0.075f;
            }
            else if (profit < 400000)
            {
                bonus = 100000 * 0.1f + (200000 - 100000) * 0.075f + (profit - 200000) * 0.05f;
            }
            else if (profit < 600000)
            {
                bonus = 100000 * 0.1f + (200000 - 100000) * 0.075f + (400000 - 200000) * 0.05f + (profit - 400000) * 0.03f;
            }
            else if (profit < 1000000)
            {
                bonus = 100000 * 0.1f + (200000 - 100000) * 0.075f + (400000 - 200000) * 0.05f + (600000 - 400000) * 0.03f + (profit - 600000) * 0.015f;
            }
            else if (profit >= 1000000)
            {
                bonus = 100000 * 0.1f + (200000 - 100000) * 0.075f + (400000 - 200000) * 0.05f + (600000 - 400000) * 0.03f + (100000 - 600000) * 0.015f + (profit - 1000000) * 0.01f;
            }

            Console.WriteLine("奖金为:" + bonus);

            Console.ReadKey();
        }
    }
}

7..某个公司采用公用电话传递数据,数据是四位的整数,在传递过程中是加密的,
加密规则如下:
每位数字 都加上5,然后用和除以10的余数代替该数字,
再将第一位和第四位交换,第二位和第三位交换。
已知一个数据6381,按以上规则加密后结果是多少

using System;

namespace cchoopDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            int number = 6381;  
            int ge = number % 10;
            int shi = number / 10 % 10;
            int bai = number / 100 % 10;
            int qian = number / 1000;

            int newGe = (qian + 5) % 10;
            int newShi = (shi + 5) % 10;
            int newBai = (bai + 5) % 10;
            int newQian = (ge + 5) % 10;

            int newNumber = newGe + newShi * 10 + newBai * 100 + newQian * 1000;
            Console.WriteLine("加密后的结果为:" + newNumber);

            Console.ReadKey();
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_34937637/article/details/80743405