• C#_switch语句,for循环,do while循环,while循环


    1:switch语句,代码如下:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication33
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("请输入分数:");
                int score = int.Parse(Console.ReadLine());
                Console.WriteLine("得分是:");
                switch (score / 10)
                {
                    case 10:
                    case 9:
                        Console.WriteLine("A");
                        break;
                    case 8:
                        Console.WriteLine("B");
                        break;
                    case 7:
                        Console.WriteLine("C");
                        break;
                    case 6:
                        Console.WriteLine("D");
                        break;
                    default:
                        Console.WriteLine("E");
                        break;
                }
            }
        }
    }
    

     运行结果:

    2:for循环,代码如下:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication34
    {
        class Program
        {
            static void Main(string[] args)
            {
                int i;
                int sum = 0;
                for (i = 0; i <= 100; i++)
                {
                    sum += i;
                }
                Console.WriteLine("最终结果是:{0}",sum);
            }
        }
    }
    

     运算结果:

     3:do while语句,代码如下:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication35
    {
        class Program
        {
            static void Main(string[] args)
            {
                int i = 0;
                int sum = 0;
                do
                {
                    sum += i;
                    i++;
                } while (i <= 100);
                Console.WriteLine("最终结果是:{0}", sum);
            }
        }
    }
    

      运行结果:

    4:while循环,代码如下:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication36
    {
        class Program
        {
            static void Main(string[] args)
            {
                int i = 0;
                int sum = 0;
                while(i<=100)
                {
                    sum += i;
                    i++;
                } 
                Console.WriteLine("最终结果是:{0}", sum);
            }
        }
    }
    

      运行结果一样。

     5:下面来一个综合的例子,为多名职员计算缴税金额。代码如下:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication37
    {
        class Program
        {
            static void Main(string[] args)
            {
                decimal sal;
                decimal salary;
                decimal tax;
                int count = 0;//记录处理数据个数
                char ch;//记录用户输入的字符
                do
                {
                    Console.Write(++count);
                    Console.Write("请输入职员工资:");
                    salary = Convert.ToDecimal(Console.ReadLine());//输入职员工资
                    sal = salary - 3500;
                    if (sal <= 0m)
                        tax = 0m;//decimal类型要加后缀m
                    else if (sal <= 1500m)
                        tax = sal * 0.03m;
                    else if (sal <= 4500m)
                        tax = sal * 0.1m-105;
                    else if (sal <= 9000m)
                        tax = sal * 0.2m-555;
                    else if (sal <= 35000m)
                        tax = sal * 0.25m-1005;
                    else if (sal <= 55000m)
                        tax = sal * 0.3m-2755;
                    else if (sal <= 80000m)
                        tax = sal * 0.35m-5505;
                    else
                        tax = sal * 0.45m-13505;
                    Console.WriteLine("应交税金为:" + tax.ToString());
                    Console.Write("继续吗?");
                    ch = Convert.ToChar(Console.ReadLine());//用户输入字符
                    //若用户输入的是除大小写Y或大小写N以外的其他字符,则要求用户重新输入
                    while (ch != 'Y' && ch != 'y' && ch !='N' && ch != 'n')
                    {
                        Console.WriteLine("对不起!只允许大小写Y或大小写N!
    继续吗?");
                        ch = Convert.ToChar(Console.ReadLine());
                    }
                }
                while (ch == 'Y'||ch == 'y');
    
                Console.WriteLine("谢谢使用!");
                Console.ReadLine();
    
            }
        }
    }
    

      运行结果:

  • 相关阅读:
    2017-3-28 javaScript DOM 操作
    2017-3-28 JavaScript 基础、语法
    2017-3-25 CSS 样式表(二)
    js判断文件是否存在的方法
    关于eclipse添加自动查找文件以及svn的插件
    关于jqgrid的一些使用
    ztree框架使用问题汇总
    bootstrap框架的使用
    UIButton 设置阴影效果
    xcode升级至9.0之后,新建xib报错: Safe Area Layout Guide Before IOS 9.0
  • 原文地址:https://www.cnblogs.com/lovemi93/p/8066250.html
Copyright © 2020-2023  润新知