zoukankan      html  css  js  c++  java
  • 编写程序,计算数组中奇数之和和偶数之和。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace odds
    {
        class Program
        {
            static void Main(string[] args)
            {
                List<string> str = new List<string>();
                int len = 0;
                int jsum =0;
                int osum =0;
                Console.WriteLine("输出数组的元素,以q结束");
                while (true)
                {
                    string input = Console.ReadLine();
                    if (input.Equals("q") == false) //如果输入的不是q(区分大小写),则增加记录
                        str.Insert(len++, input);
                    else
                        break;
                }
    
                //Console.WriteLine("输出数据长度");
               // Console.WriteLine(len); //结果说明数据是按行存在链表中的,每行占链表一个值
               // Console.WriteLine("依次输出链表中数据");
               // for (int i = 0; i < len; i++)
               // {
                //    Console.WriteLine(str[i]); //依次输出链表每个值,也是依次输出每行
                //}
                //Console.WriteLine("依次输出每个值");
                string[][] every = new string[len][]; //交叉数组,行固定,为上面得到的行数,每一行的长度不定(每行字符间以空格或其他分割)
                for (int i = 0; i < len; i++)
                {
                    every[i] = str[i].Split(); //C#对空格的分割方式之一,如果是其他分割方式,就需要也使用上面的链表分割每行的方式了
                }
                //for (int i = 0; i < len; i++)
                //{
                //    for (int j = 0; j < every[i].Length; j++)
                  //  {
                 //       Console.WriteLine(every[i][j]);
                //    }
              //  }
                
                for (int i = 0; i < len; i++)
                {
                    for (int j = 0; j < every[i].Length; j++)
                    {
                        int aa;
                       // Console.WriteLine(every[i][j]);
                        aa = int.Parse(every[i][j]);
                        if ((aa % 2) == 1)
                        {
                            jsum += aa;
                        }
                        else {
                            osum += aa;
                        }
                    }
                }
                Console.WriteLine("奇数之和为:");
                Console.WriteLine(jsum);
                Console.WriteLine("偶数之和为:");
                Console.WriteLine(osum);
                Console.ReadKey();
            }
        }
    }

  • 相关阅读:
    mysql练习题
    转 -day19--form&modelform
    day20--注册功能及首页
    day19-form表单&auth模块、项目初识
    day15-pymysql模块的使用
    第14天jquery+bootstrap
    第13天-js+jquery
    iTween基础之Punch(摇晃)
    iTween基础之Audio(音量和音调的变化)
    iTween基础之Rotate(旋转角度)
  • 原文地址:https://www.cnblogs.com/a155-/p/14001880.html
Copyright © 2011-2022 走看看