zoukankan      html  css  js  c++  java
  • 求奇数偶数的和,,利用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();
            }

  • 相关阅读:
    eval(data)和eval("("+data+")")的区别
    来自 119.*.*.*的回复: TTL 传输中过期
    小伙伴们,我们一起奋斗吧
    计算机专业学习课程推荐
    sysbench使用教程【转载】
    Docker学习笔记 — Docker私有仓库搭建【转载】
    Linux设置静态IP【转】
    聊一聊PV和并发、以及计算web服务器的数量的方法【转】
    Linux常用Shell脚本珍藏【转载】
    戏说云计算之PaaS,IaaS,SaaS【转载】
  • 原文地址:https://www.cnblogs.com/lk-kk/p/4387971.html
Copyright © 2011-2022 走看看