我正在学习c#,并且有家庭作品在控制台应用程序中制作简单字典。
我编写了所有代码并且应该可以正常工作,但在其中一个for循环中,程序会抛出Error:System.IndexOutofRange
。
我尝试了所有我知道的(我只是初学者所以我不太了解)但它总是给出错误。
该程序的主要思想是用户必须输入他希望使用多少单词然后输入他的语言中的单词(我的希伯来语,因此在程序中是希伯来语)和英语,它将单词保存在两个deffernt Arrays中但是在相同的索引中。然后程序要求用户以他的语言输入句子然后程序在句子上运行找到单词(每次程序看到空格它开始一个新单词),然后用另一个for循环它看起来在希伯来语数组中,如果它找到匹配放置在同一索引但在英文数组中的单词,如果没有找到,它会写出原始单词。
static void Main(string[] args)
{
Console.WriteLine("Enter the number of words you wish to use");
int wordsNumber = int.Parse(Console.ReadLine());
string[] hebrew = new string[wordsNumber];
string[] english = new string[wordsNumber];
for (int i = 0; i < wordsNumber; i++)
{
Console.WriteLine("Enter word in Hebrew.");
hebrew[i] = Console.ReadLine();
Console.WriteLine("Now enter the translate of {0} in English.",hebrew[i]);
english[i] = Console.ReadLine();
}
Console.WriteLine("Now enter the sentence that you want to translate..and press ENTER");
string searchSentence = Console.ReadLine();
string translateWord = "";
string result = "";
while(searchSentence != "stop")
{
for (int i = 0; i < searchSentence.Length;i++ )
{
translateWord = "";
while ((searchSentence[i] == ' ')&&(searchSentence.Length > i))//Skip the spaces in the start of the sentence
i++;
while ((searchSentence[i] != ' ') && (searchSentence.Length > i))//Here is the problem when with the OutOfRnge
{
translateWord += searchSentence[i];
i++;
}
for(int j = 0;j<hebrew.Length ;j++)
{
if (translateWord == hebrew[j])
{
result += english[j];
}
else
{
result += translateWord[i];
}
result += " ";
}
}
Console.WriteLine(result);
Console.WriteLine("Enter new sentence for translate or enter 'stop' to EXIT the programm");
searchSentence = Console.ReadLine();
}
答案 0 :(得分:2)
while ((searchSentence[i] != ' ') && (searchSentence.Length > i))
考虑一下你习惯阅读的方式(从树林的右边到左边)会影响你编写代码的方式。此代码是向后的,都按照&amp;&amp;和操作数和长度测试。因此,没有利用&amp;&amp; amp;的&em;&amp;&amp;;运营商。可能很难忘记,这是你必须要做的事情。
使用以下命令避免此异常:
while ((i < searchSentence.Length) && (searchSentence[i] != ' '))
确保searchSentence [i]不会抛出IndexOutOfRangeException。
答案 1 :(得分:1)
你的代码非常令人困惑,很难分辨出错误在哪里。但你最好还是构建一个充满翻译的词典然后再使用它。我已经省去了错误处理,你可以扩展,但这里有一个完全相同的例子 - 但是有一个字典
Dictionary<string, string> words = new Dictionary<string, string>();
Console.WriteLine("Enter the number of words you wish to use");
int wordsNumber = int.Parse(Console.ReadLine());
for (int i = 0; i < wordsNumber; i++)
{
Console.WriteLine("Enter word in Hebrew.");
string hebrew = Console.ReadLine();
Console.WriteLine("Now enter the translate of {0} in English.", hebrew);
string english = Console.ReadLine();
words.Add(hebrew, english);
}
Console.WriteLine("Now enter the sentence that you want to translate..and press ENTER");
string searchSentence = Console.ReadLine();
string[] splitSentence = searchSentence.Split(new char[]{' '});
string result = "";
foreach (string s in splitSentence)
result += string.Format("{0} ", words[s]);
Console.WriteLine(result);
Console.ReadLine();
答案 2 :(得分:0)
看着
while(searchSentence != "stop")
{
for (int i = 0; i < searchSentence.Length;i++ )
{
translateWord = "";
while ((searchSentence[i] == ' ')&&(searchSentence.Length > i))//Skip the spaces in the start of the sentence
i++; //updated here and used on nex line when i could be greater than SearchSentence.Length now
while ((searchSentence[i] != ' ') && (searchSentence.Length > i))//Here is the problem when with the OutOfRnge
{
translateWord += searchSentence[i];
i++; // updated here
}
for(int j = 0;j<hebrew.Length ;j++)
{
if (translateWord == hebrew[j])
{
result += english[j];
}
else
{
result += translateWord[i];
}
result += " ";
}
}
你在for循环中增加i
。如果它等于或大于searchSentence.Length
,那么你使用它会抛出你得到的错误
修改以修复此更改
for (int i = 0; i < searchSentence.Length;i++ )
{
translateWord = "";
while ((searchSentence[i] == ' ')&&(searchSentence.Length > i))//Skip the spaces in the start of the sentence
i++;
while ((searchSentence[i] != ' ') && (searchSentence.Length > i))//Here is the problem when with the OutOfRnge
{
translateWord += searchSentence[i];
i++;
}
for(int j = 0;j<hebrew.Length ;j++)
{
if (translateWord == hebrew[j])
{
result += english[j];
}
else
{
result += translateWord[i];
}
result += " ";
}
}
到这个
for (int i = 0; i < searchSentence.Length;i++ )
{
if(((searchSentence[i] != ' ')&&(searchSentence.Length > i)))
{
while ((searchSentence[i] != ' ') && (searchSentence.Length > i))//Here was the problem when with the OutOfRnge
{
translateWord += searchSentence[i];
i++; // updated here
if(i>=searchSentence.Length)
{
break;
}
}
if(i>=searchSentence.Length)
for(int j = 0;j<hebrew.Length ;j++)
{
if (translateWord == hebrew[j])
{
result += english[j];
}
else
{
result += translateWord[i];
}
result += " ";
}
}
}