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

    using System;
    using System.Collections.Generic;
    
    namespace Test_1_4
    {
        class Program
        {
            static void Main(string[] args)
            {
                List<string> str = new List<string>();
                int len = 0;
                int oddsum = 0;
                int evensum = 0;
                Console.WriteLine("请输入数组:");
                while (true)
                {
                    string input = Console.ReadLine();
                    if (input.Equals("") == false)//如果不是输入为空则增加输入记录
                        str.Insert(len++, input);
                    else
                        break;
    
                }
                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++)
                    {
                        int aa;
                        aa = int.Parse(every[i][j]);
                        if ((aa % 2) == 1)
                        {
                            oddsum += aa;
                        }
                        else
                        {
                            evensum += aa;
                        }
                    }
                }
                Console.WriteLine("奇数之和等于:");
                Console.WriteLine(oddsum);
                Console.WriteLine("偶数之和等于:");
                Console.WriteLine(evensum);
                Console.ReadKey();
            }
        }
    }

    截图

  • 相关阅读:
    IDEA操作git的一些常用技巧
    实现多Realm时,可能会出现的问题
    Solr入门-Solr服务安装(windows系统)
    ES6中的Set和Map集合
    ES6中的类
    ES6数组扩展
    ES6定型数组
    Promise和异步编程
    深入理解ajax系列第八篇
    深入理解ajax系列第六篇
  • 原文地址:https://www.cnblogs.com/dty602511/p/15417355.html
Copyright © 2011-2022 走看看