我非常抱歉,一位朋友建议我在这里发表我的问题,以寻求帮助,但我还是编程的新手,我的问题是我必须编写一个计算次数的程序 (频率)重复一个字母(大写或小写),并计算最大频率及其含义。没有错误,并且可以正常编译,但是执行时显示此“未处理的异常”,我不知道该怎么办,它说 第16行 Char C = (Char)(Chararray[x]);
using System;
namespace ConsolApp3
{
class Program
{
static void Main(string[] args)
{
int[] number = new int[26];
Console.WriteLine("Input your statement:");
System.String mystring = Console.ReadLine();
Char[] Chararray = new Char[mystring.Length];
Chararray = mystring.ToCharArray();
int x = 0;
for (x = 0; x < Chararray.Length; x++) ;
Char C = (Char)(Chararray[x]);
C = toUpper(C);
if ((int)(C) >= 65 && (int)(C) < 91)
{
number[(int)(C) - 65]++;
}
for (int y = 0; y < number.Length; y++) ;
if (number[1] > 0)
{
Console.WriteLine((Char)(x + 65) + ":" + number[x]);
}
int max = Max(number);
Console.WriteLine("The Maximum frequncy is " + max);
Console.WriteLine("The letters are ");
}
static Char toUpper(Char C)
{
if ((int)(C) >= 97 && (int)(C) < 123)
{
C = (Char)((int)(C) - 32);
}
return C;
}
static int Max(int[] array)
{
int max = array[0];
for (int z = 1; z < array.Length; z++)
{
if (max < array[z])
{
max = array[z];
}
}
return max;
}
}
}
这是我尝试执行时的错误消息:
"Unhandled Exception: System.IndexOutOfRangeException: Index was outside the bounds of the array.at ConsolApp3.Program.Main(String[] args) in C:\Users\Mosaab\source\repos\ConsoleApp3\Program.cs:line 16"