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

    截图

  • 相关阅读:
    File操作
    集合
    几个python资料地址
    Case1-用list写shoppingcart
    字符串操作
    运算-Dictionary
    运算-list
    Python数据类型
    标准库和库导入
    Pycharm
  • 原文地址:https://www.cnblogs.com/dty602511/p/15417355.html
Copyright © 2011-2022 走看看