无法理解为什么 for 循环超出范围
Cant understand why the for loop get out of range
我正在学习 c#,并且有家庭作业可以在控制台应用程序中制作简单的字典。
我编写了所有代码,它应该可以正常工作,但在其中一个 for 循环中,程序抛出 Error:System.IndexOutofRange
。
我尝试了所有我知道的(我只是初学者所以我不太了解)但它总是给出错误。
该程序的主要思想是用户必须输入他希望使用的单词数量,然后用他的语言(我的是希伯来语,所以在程序中是希伯来语)和英语输入单词,并将单词保存在两个不同的数组中,但是在同一个索引中。然后程序要求用户用他的语言输入句子,然后程序在句子上运行以查找单词(每次程序看到 space 它开始一个新单词),然后使用另一个 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();
}
正在查看
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 += " ";
}
}
}
你的代码非常混乱,很难说出错误在哪里。但是你最好构建一个充满翻译的字典然后使用它。我已经遗漏了错误处理供您扩展,但这是一个完全相同的示例 - 但使用字典
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();
while ((searchSentence[i] != ' ') && (searchSentence.Length > i))
考虑一下您习惯阅读的方式(在您的脖子上从右到左)正在影响您编写代码的方式。此代码是向后的,无论是在 && 操作数的顺序还是在长度测试中。因此没有利用 && 运算符的 短路 行为。可能很难忘记,你必须努力。
通过以下方式避免此异常:
while ((i < searchSentence.Length) && (searchSentence[i] != ' '))
这确保 searchSentence[i] 不会抛出 IndexOutOfRangeException。
我正在学习 c#,并且有家庭作业可以在控制台应用程序中制作简单的字典。
我编写了所有代码,它应该可以正常工作,但在其中一个 for 循环中,程序抛出 Error:System.IndexOutofRange
。
我尝试了所有我知道的(我只是初学者所以我不太了解)但它总是给出错误。
该程序的主要思想是用户必须输入他希望使用的单词数量,然后用他的语言(我的是希伯来语,所以在程序中是希伯来语)和英语输入单词,并将单词保存在两个不同的数组中,但是在同一个索引中。然后程序要求用户用他的语言输入句子,然后程序在句子上运行以查找单词(每次程序看到 space 它开始一个新单词),然后使用另一个 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();
}
正在查看
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 += " ";
}
}
}
你的代码非常混乱,很难说出错误在哪里。但是你最好构建一个充满翻译的字典然后使用它。我已经遗漏了错误处理供您扩展,但这是一个完全相同的示例 - 但使用字典
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();
while ((searchSentence[i] != ' ') && (searchSentence.Length > i))
考虑一下您习惯阅读的方式(在您的脖子上从右到左)正在影响您编写代码的方式。此代码是向后的,无论是在 && 操作数的顺序还是在长度测试中。因此没有利用 && 运算符的 短路 行为。可能很难忘记,你必须努力。
通过以下方式避免此异常:
while ((i < searchSentence.Length) && (searchSentence[i] != ' '))
这确保 searchSentence[i] 不会抛出 IndexOutOfRangeException。