zoukankan      html  css  js  c++  java
  • Net基础篇_学习笔记_第九天_数组_三个练习

    练习一:

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace _数组练习01
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             string str = null;
    14             string[] names = { "老杨","老李","老赵","老孙","老沈"};
    15             for (int i = 0; i < names.Length; i++)
    16             {
    17                 str += names[i] + "|";
    18             }
    19             Console.WriteLine(str);
    20             Console.ReadKey();
    21         }
    22     }
    23 }

    优化为:

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace _数组练习01
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             string str = null;
    14             string[] names = { "老杨","老李","老赵","老孙","老沈"};
    15             for (int i = 0; i < names.Length-1; i++)
    16             {
    17                 str += names[i] + "|";
    18             }
    19             Console.WriteLine(str+names[names.Length-1]);
    20             Console.ReadKey();
    21         }
    22     }
    23 }

     练习二:如遇正数,将这个位置元素的值加1,如遇负数,将这个位置元素的值减1,如遇0,则不变。

    int nums={0,5,-8,-41,5,94,-1,-22};

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace _数组练习01
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             int[] nums = { 66,58,841,-556,-121,-541,55};
    14             for (int i = 0; i < nums.Length; i++)
    15             {
    16                 if (nums[i] > 0)
    17                 {
    18                     nums[i] += 1;
    19                 }
    20                 else if (nums[i] < 0)
    21                 {
    22                     nums[i] -= 1;
    23                 }
    24             }
    25             for (int i = 0; i < nums.Length; i++)
    26             {
    27                 Console.WriteLine(nums[i]);
    28             } 
    29             Console.ReadKey();
    30         }
    31     }
    32 }

     联系三:将一个字符串数组的元素顺序进行反转

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace _数组练习01
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             string[] names  = { "a", "b", "c" ,"d"};
    14             for (int i = 0; i <names.Length/2; i++)
    15             {
    16                 //交换两个变量最简单的方式就是声明第三方的一个变量。
    17                 string _temp = names[i];
    18                 names[i] = names[names.Length - 1 - i];
    19                 names[names.Length - 1 - i] = _temp;
    20             }
    21             for (int i = 0; i < names.Length; i++)
    22             {
    23                 Console.WriteLine(names[i]);
    24             }
    25             Console.ReadKey();
    26         }
    27     }
    28 }
  • 相关阅读:
    课程作业四 生成随机数并求和,大数运算
    课程作业三 string,char操作
    课程作业二 类内静态内容(代码块,静态变量),构造函数,非静态代码块执行顺序
    十一作业 java数值范围方面训练
    课程作业一 将字符串型数组里的数字相加
    NABCD需求分析
    人月神话阅读笔记01
    软件工程第五周总结
    清明第三天
    清明第二天安排
  • 原文地址:https://www.cnblogs.com/NBOWeb/p/7298309.html
Copyright © 2011-2022 走看看