循环练习

练习1

请输入一个字母例如:f,则输出abcdefedcba

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

namespace 练习1
{
    class Program
    {
        static void Main(string[] args)
        {


            Console.Write("请输入一个字母:");
            char a = Convert.ToChar(Console.ReadLine());

            for (char b = 'a'; b != a; b++)
            {
                Console.Write(b + ",");


            }
            char c = a;
            for (int i = 1; i <= 26; i++)
            {
                Console.Write(c + ",");

                if (c == 'a')
                {
                    break;
                }
                c--;
            }

            
         


            Console.ReadLine();
        }
    }
}

 

练习2

请输入三个0-20之间的正整数

然后求和

输入错误不计

 

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

namespace 练习9
{
    class Program
    {
        static void Main(string[] args)
        {
            int b = 0;
            for (; ; )
            {
                Console.Write("请输入一个正整数(0-20)");
                int i = Convert.ToInt32(Console.ReadLine());

                
                
                    if (i < 0 || i > 20)
                    {

                        Console.Write("输入错误!");
                      
                    }

                    else
                    {
                        

                        int sum = 0;
                        for (int a = 0; a <= i; a++)
                        {
                            sum += a;

                        }
                        Console.Write(sum);
                        if (b == 2)
                        {
                            break;
                        }
                        b++;
                    }

                

            }



                Console.ReadLine();
        }
    }
}

 

posted @ 2016-05-24 08:36  宋小尧  阅读(105)  评论(0编辑  收藏  举报