zoukankan      html  css  js  c++  java
  • 34数组 简单

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    /*数组 保存多个值,几乎任意类型都可以声明数组
     * int[] nums = {5,3,8};
     * 个数和声明数必须一致 
     * int[] nums = new int[3];
     * 使用索引器访问指定编号位置的元素,访问数组元素: nums[0],nums[1].索引从0开始,取到元素的类型就是数组元素的类型,可以数组元素进行赋值
     * 
     * foreach循环
     * string[] names = {"tom","jerry","lily"}
     * foreach(string name in names){
     *    console.writeLine("");
     * }
     */
    namespace _34数组
    {
        class Program
        {
            static void Main(string[] args)
            {
                //练习一: 从一个整数数组中取出最大的整数
                /*int[] nums = {44,22,55,99,10,44};
                int max = 0;
                for (int i = 0; i < nums.Length; i++) {
                    if (max <= nums[i])
                        max = nums[i];
                }
                Console.WriteLine("数组中的最大值是:{0}",max);*/
    
                //练习二:记算一个整数数组的所有元素的和
                int[] nums = { 44, 22, 55, 99, 10, 44 };
                int sum = 0;
                for (int i = 0; i < nums.Length; i++) {
                    sum += nums[i];
                }
                Console.WriteLine("数组中的总和值是:{0}", sum);
    
                string str = "";
                //将一个字符串数组输出为|分割的形式,比如"梅西|卡卡|郑大世"
                for (int i = 0; i < nums.Length; i++)
                {
                    //sum += nums[i];
                    if (i == nums.Length-1)
                    {
                        str += nums[i]; 
                    }else {
                        str += nums[i] + "|";
                    }
                }
                Console.WriteLine("数组后的字符串为:{0}", str);
    
                //练习4: 将一个整数数组的每一个元素地如下的处理,如果元素是正数则将指定位置的元素的值翻翻
                //如果元素是负数则将指定的绵值取相反数
                /*int[] intfour = {44,33,22,-1,-9,-12};
                for (int i = 0; i < intfour.Length; i++) {
                    if (intfour[i] >= 0) {
                        intfour[i] =  intfour[i] * 2;
                    }else if (intfour[i] < 0) {
                        intfour[i] = -(intfour[i]); //靠,真不好意思说这里不会的这点
                    } 
                }
                Console.WriteLine("处理后的数组值为:");
                for (int i = 0; i < intfour.Length; i++) {
                    Console.WriteLine(intfour[i]+" ");
                }*/
    
                //练习5: 有一个整数数组,请声明一个字符串数组,将整数数组中的第一个元素的值转换为字符串保存到字符串数组中
                /*int[] intfour = { 44, 33, 22, -1, -9, -12 };
                string[] strfour = new string[intfour.Length];
                for (int i = 0; i < intfour.Length; i++) {
                    strfour[i] = Convert.ToString(intfour[i]);
                }
                for (int i = 0; i < strfour.Length; i++) {
                    Console.WriteLine("第{0}个元素是{1}",i,strfour[i]);
                }*/
    
                //练习6: 将一个字符串数组的元素的顺序进行返转
                string[] sixstr = { "aa", "bb", "dd", "ff","gg"};
                //这里计算的方法是把第i个元素的值,与lenght-i-1的值进行对调
                for (int i = 0; i < sixstr.Length/2; i++) {
                    string a = sixstr[i];
                    sixstr[i] = sixstr[sixstr.Length - i - 1];
                    sixstr[sixstr.Length - i - 1] = a;
    
                    //Console.WriteLine("第{0}个元素是{1}", i, sixstr[i]);
                }
                for (int i = 0; i < sixstr.Length; i++)
                {
                    Console.WriteLine("第{0}个元素是{1}", i, sixstr[i]);
                }
                foreach (string _str in sixstr)
                {
                    Console.WriteLine("元素为{0}", _str);
                }
    
                    Console.ReadKey();
            }
        }
    }
    

      

  • 相关阅读:
    Vue 组件封装发布到npm 报错 Uncaught TypeError: Cannot read property 'toLowerCase' of undefined
    SpringBoot thymeleaf模板版本,thymeleaf模板更换版本
    SpringBoot application.properties (application.yml)优先级从高到低
    SpringBoot Cmd运行Jar文件指定active文件的命令如下
    Linux各文件夹的作用
    spring + Mybatis + pageHelper + druid 整合源码分享
    MyEclipse weblogic Deploy Location项目名称不正确解决方案
    Mybatis整合通用Dao,Mybatis整合通用Mapper,MyBatis3.x整合通用 Mapper3.5.x
    An internal error occurred during: "Launching xxx on WebLogic10.x".
    Spring集成Mybatis,spring4.x整合Mybatis3.x
  • 原文地址:https://www.cnblogs.com/xiangxiaodong/p/2358718.html
Copyright © 2011-2022 走看看