C# System.ArgumentOutOfRangeException在我的word搜索程序中

C# System.ArgumentOutOfRangeException在我的word搜索程序中,c#,C#,我不明白这里出了什么问题。有人能解释一下吗 using System; public class Test { public static void Main() { int bobo = 0; string result = ""; string bob; string search = Console.ReadLine();

我不明白这里出了什么问题。有人能解释一下吗

using System;

public class Test
{
        public static void Main()
        {
                int bobo = 0;
                string result = "";
                string bob;
                string search = Console.ReadLine();
                string words = Console.ReadLine();
                string first = words.Substring(0,1);
                string second = words.Substring(1,2);
                string third = words.Substring(2,3);
                for(int i = 0;i<searchc.Length;i++)
                {
                        bobo++;
                        bob = search.Substring(bobo,bobo+2);    
                        if(bob == first)
                        {
                                result += bob.ToUpper();
                        }
                }

                Console.WriteLine(result);
        }
}
使用系统;
公开课考试
{
公共静态void Main()
{
int-bobo=0;
字符串结果=”;
弦摆;
字符串搜索=Console.ReadLine();
string words=Console.ReadLine();
第一个字符串=单词。子字符串(0,1);
字符串秒=单词。子字符串(1,2);
第三个字符串=单词。子字符串(2,3);
对于(int i=0;i此长度)
参数名称:长度
在System.String.Substring(Int32 startIndex,Int32 length)[0x00000]处输入:0
在0中的Test.Main()[0x00000]处

我恐怕无法帮您解决您的alg。看起来有缺陷

我会首先添加一个检查,以确保长度大于2,然后下面的逻辑应该会有所帮助


将bobo更改为i,并将for循环更新为
for(int i=0;i我恐怕无法帮助您检查您的alg。看起来有缺陷

我会首先添加一个检查,以确保长度大于2,然后下面的逻辑应该会有所帮助


将bobo更改为i并更新for for for <代码>(int i=0;i我会考虑为您的“Word”对象做一个String .Strut.()。

这取决于输入的格式,但例如:

文字: “word1 word2 word3”

您可以执行以下操作:

var words = Console.ReadLine().Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries);
如果您的“单词”只是一个字符列表,那么我会首先通过更好地命名字符串来让您的代码更清晰。很难理解您试图用提供的示例做什么。但下面是我要做的

        var result = string.Empty;

        var search = Console.ReadLine();
        var words = Console.ReadLine();
        var first = words.Substring(0, 1);
        for (int i = 0; i < search.Length; i++)
        {
            if (i + 1 > search.Length) break;
            var bob = search.Substring(i, i + 2);

            if (bob == first)
                result += bob.ToUpper();
        }

        Console.WriteLine(result);

希望它有帮助。

< P>我会考虑为你的“Word”对象做一个String .Strut.()。

这取决于输入的格式,但例如:

文字: “word1 word2 word3”

您可以执行以下操作:

var words = Console.ReadLine().Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries);
如果您的“单词”只是一个字符列表,那么我会首先通过更好地命名字符串来让您的代码更清晰。很难理解您试图用提供的示例做什么。但下面是我要做的

        var result = string.Empty;

        var search = Console.ReadLine();
        var words = Console.ReadLine();
        var first = words.Substring(0, 1);
        for (int i = 0; i < search.Length; i++)
        {
            if (i + 1 > search.Length) break;
            var bob = search.Substring(i, i + 2);

            if (bob == first)
                result += bob.ToUpper();
        }

        Console.WriteLine(result);

希望有帮助。

可能您的某些单词的长度与您期望的长度不符;例如,如果您的单词长度为1个字母,word.Substring(0,2)将失败,因为它将超出字符串的长度-“ArgumentOutOfRangeException”。可能您的某些单词的长度与您预期的长度不符;例如,如果您的单词长度为1个字母,则word.Substring(0,2)将失败,因为它将超出字符串的长度-“ArgumentOutOfRangeException”.Gee,非常感谢!这只是我在午餐休息时拼凑的东西,所以它不完全完美我缩进了我的代码!:)Gee,非常感谢!这只是我在午餐休息时拼凑的东西,所以它不完全完美我缩进了我的代码!:)
        var result = new System.Text.StringBuilder();

        var search = Console.ReadLine();
        var words = Console.ReadLine().Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries);

        foreach (var word in words)
        {
            if (search.Contains(word))
                result.Append(string.Format("{0},", word));
        }

        Console.WriteLine(result.ToString());