c#单词个数,加¥,最长单词,平均值

在这里插入图片描述

 static int count(string sentence)
        {
    
    
            int count = 0;
            string[] words = new string[100];
            words = sentence.Split(' ');
            for (int i = 0; i < words.Length; i++)
            {
    
    
                if (words[i] != "")
                    count++;
            }
            return count;
        }
            string sent = Console.ReadLine(); ;
            Console.WriteLine("{0}里面共有{1}个单词", sent, count(sent));

在这里插入图片描述

在这里插入图片描述

public static string AddDollar(string sentence)
        {
    
    
            string st = string.Empty;
            for (int i = 0; i < sentence.Length; i++)
            {
    
    
                if (char.IsLetter(sentence[i]))
                {
    
    
                    st += sentence[i] + "$";
                }
                else
                {
    
    
                    st += sentence[i];
                }
            }
            return st;
        }
            string st = Console.ReadLine();
            Console.WriteLine("加¥后结果为{0}", AddDollar(st));

在这里插入图片描述

找最长单词

static string getLongWord(string sentense)
        {
    
    
            string word = "";
            string[] words = new string[100];
            words = sentense.Split(' ');
            for (int i = 0; i < words.Length; i++)
            {
    
    

                if (word.Length < words[i].Length)
                {
    
    
                    word = words[i];
                }
            }
            return word;
        }
  string st = Console.ReadLine();
  Console.WriteLine("最长的单词是:{0}", getLongWord(st));

在这里插入图片描述
在这里插入图片描述

 public static void getMaxMinAverSum(out int max, out int min, out double aver, out int sum)
        {
    
    
            int[] nums = new int[10];
            max = 0;
            min = 50;
            sum = 0;
            Random rnd = new Random();
            for (int i = 0; i < nums.Length; i++)
            {
    
    
                nums[i] = rnd.Next(20, 51);
                if (max < nums[i])
                    max = nums[i];
                if (min > nums[i])
                    min = nums[i];
                sum += nums[i];
                Console.Write("{0},", nums[i]);
            }
            aver = (double)sum / nums.Length;
        }
            int max, min, sum;
            double aver;
            getMaxMinAverSum(out max, out min, out aver, out sum);
            Console.WriteLine("最大值{0},最小值{1},平均值{2},和{3}", max, min, aver, sum);

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_41920585/article/details/110285353