c#arrayindex中的字符串操作超出范围异常为什么会发生? 使用System.IO; 使用制度; 班级计划 { 静态void Main() { 字符串s1=“iamamamok”; 字符串s2=“mam”; int i=0,j=0; int count=0,countchar=0; 而(j

c#arrayindex中的字符串操作超出范围异常为什么会发生? 使用System.IO; 使用制度; 班级计划 { 静态void Main() { 字符串s1=“iamamamok”; 字符串s2=“mam”; int i=0,j=0; int count=0,countchar=0; 而(j,c#,string,substring,indexoutofboundsexception,C#,String,Substring,Indexoutofboundsexception,则在内部循环中递增j,然后再次使用它,而不检查它是否小于字符串的长度 (为了将来的参考,请发布错误消息,包括错误发生的那一行,因为这有助于其他人调试)你能说明你希望你的程序做什么吗?因为你的代码不是问题中的代码,所以这段代码卡在无限循环中。这段程序运行在无限循环中,而不是索引超出范围异常 using System.IO; using System; class Program { static void Main() { string s1="iamamamok

则在内部循环中递增
j
,然后再次使用它,而不检查它是否小于字符串的长度


(为了将来的参考,请发布错误消息,包括错误发生的那一行,因为这有助于其他人调试)

你能说明你希望你的程序做什么吗?因为你的代码不是问题中的代码,所以这段代码卡在无限循环中。这段程序运行在无限循环中,而不是索引超出范围异常
using System.IO;
using System;

class Program
{
    static void Main()
    {
        string s1="iamamamok";
        string s2="mam";
        int i=0,j=0;
        int count=0,countchar=0;

        while(j<s2.Length)
        {
            while(i<s1.Length)
            {
                if(s1[i]==s2[j])
                {
                    countchar++;
                    j++;
                    if(countchar==(s2.Length))
                    {
                        count +=1;
                        j=0;
                        countchar=0;
                    }

                }
                else
                {
                    j=0;
                    countchar=0;
                }
                i++;
                if(i==s1.Length)
                {
                    break;
                }
            }
        }
    Console.Write(count);

    }
}