码迷,mamicode.com
首页 > 其他好文 > 详细

求奇数偶数的和,,利用while循环

时间:2015-04-02 20:51:02      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:

 static void Main(string[] args)
        {
             while (true)
                {
             try
            {
              
                    Console.Write("请输入一个数:");
                    int n = Convert.ToInt32(Console.ReadLine());
                    int i = 1; int s = 0, z = 0;
                    while (i <= n)
                    {
                        if (i % 2 == 1)
                        {

                            Console.Write(i + "为奇数");
                            s = s + i;
                        }
                        else
                        {
                            Console.WriteLine(i + "为偶数");
                            z = z + i;
                        }
                        i++;
                    }
                    Console.WriteLine("奇数和为" + s);
                    Console.WriteLine("偶数和为" + z);
                  
                }
           
            catch (Exception)
            {

                Console.WriteLine("请输入正确的数字");

            }
            finally
            {
                Console.WriteLine("约里克");
            }
            }Console.ReadLine();
        }

求奇数偶数的和,,利用while循环

标签:

原文地址:http://www.cnblogs.com/lk-kk/p/4387971.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!