为什么定义的字符串最多只能输入3个字符,否则就会出错呢?

//统计一串英文字符串的元音字母的个数

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;


namespace _4_3

{

    class Program

    {

        static void Main(string[] args)

        {

            Console.WriteLine("输入一串字符串");

            string s = Console.ReadLine();

            string a="a";

            string e="e";

            string i="i";

            string o="o";

            string u="u";

            int count=0;

            for (int j = 0; j < s.Length -1; j++)

            {

                if (a.Equals(s.Substring(j, j + 1)) | e.Equals(s.Substring(j, j + 1)) | i.Equals(s.Substring(j, j + 1)) | o.Equals(s.Substring(j, j + 1)) | u.Equals(s.Substring (j,j+1)))

                    count++;

            }

            Console.WriteLine("元音字母个数为:" + count);

        }

    }

}


三色堇忞
浏览 1258回答 1
1回答

一毛钱

出现这个的问题是你的理解有问题,Substring(开始值,长度)不是开始值和结束值,应该这样写                if (a.Equals(s.Substring(j, 1)) | e.Equals(s.Substring(j, 1)) | i.Equals(s.Substring(j, 1)) | o.Equals(s.Substring(j, 1)) | u.Equals(s.Substring(j, 1)))                     count++;
打开App,查看更多内容
随时随地看视频慕课网APP