C#在数组中查找最短和最长的单词 [英] C# finding the shortest and longest word in a array

查看:255
本文介绍了C#在数组中查找最短和最长的单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图根据长度和最短时间找到最短和最长的字符串值.到目前为止,脚本在写行之后退出.我认为代码需要一些帮助,我不认为for循环可以自行工作. 任何援助将不胜感激.

I am trying to find the shortest and longest string value based on length and im getting stuck. As of now the script exits after the writeline. I think the code needs some help, I dont think a for loop can work on its own. Any assistance would be appreciated.

        for (int i = 5; i <0; i++)
        {
            string[] word = new string[5];
           Console.WriteLine("Type in a word");
            word[i] = Console.ReadLine();

             int length = word[i].Length;
             int min = word[0].Length;
             int max = word[0].Length;
             string maxx;
             string minn;


              if (length > max)
                 {
                   maxx = word[i];
                   Console.Write("Shortest");
                  }
             if (length < min) 
              {
                 minn = word[i];
                Console.Write("Longest");
              }



         }
        Console.ReadKey(true);
    }

推荐答案

string[] word = new string[5];
for (int i = 0; i <= word.Length; i++)
{

    Console.WriteLine("Type in a word");
    word[i] = Console.ReadLine();
}
int min = word[0].Length;
int max = word[0].Length;
string maxx = "";
string minn = "";
for (int i = 0; i <= word.Length; i++)
{
    int length = word[i].Length;
    if (length > max)
    {
        maxx = word[i];
    }
    if (length < min)
    {
        minn = word[i];
        Console.Write("Longest");
    }
}
Console.Write("Shortest:" + maxx);
Console.Write("Longest" + minn);
Console.ReadKey(true);

这篇关于C#在数组中查找最短和最长的单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆