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

 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();
        }

posted @ 2015-04-02 19:06  XCml  阅读(1161)  评论(0编辑  收藏  举报