-4

我有两个字符串列表(girlsLIST1 和 GirlsLIST2),其中我有 5 个字符串值,但是当我将这些值增加到 6 时,我得到一个异常,这是我的代码。现在我有基于问题变量值的问题变量,for循环运行并从列表中选择一些随机字符串并将这些结果存储在girl1names和girl2names列表中。目前我的问题变量的值为 5,但如果我将 GirlsLIST1 和 GirlsLIST2 列表中的字符串增加到 5 以上,那么我会得到一个异常。

List<string> girlsLIST1 = new List<string> { "Aliya", "Manal", "Ayman", "Laiba", "Lubna", "Aziza" };
List<string> girlsLIST2 = new List<string> { "Mehmoona", "Samra", "Saria", "Aleena", "Faiza","Mehwish" };
List<string> girl1names = new List<string>();
List<string> girl2names = new List<string>();
int GirlName;
int GirlName2;
if (addquestions == true)
{
    //for (int i = 0; i <girlsLIST1.Count; i++)
    //{
    //    int Girlsnames1 = random.Next(girlsLIST1.Count);
    //    GirlName = girlsLIST1[Girlsnames1];
    //}
    //for (int i = 0; i < girlsLIST2.Count; i++)
    //{
    //    int Girlsnames2 = random.Next(girlsLIST2.Count);
    //    GirlName2 = girlsLIST2[Girlsnames2];
    //}
    var random = new Random();
    int min = 2;
    int max = 10;

    for (int i = 0; i < questions; i++)
    {
        GirlName = random.Next(girlsLIST1.Count);
        GirlName2 = random.Next(girlsLIST2.Count);
        girl1names.Add(girlsLIST1[GirlName]);
        girl2names.Add(girlsLIST2[GirlName2]);
    }

    var list = new List<string>
    {
        girl1names[GirlName] +" was playing basketball. " + random.Next(min, max) + " of her shots went in the hoop. " + random.Next(min, max) + " of her shots did not go in the hoop. How many shots were there in total?",
        "The restaurant has " + random.Next(min, max) + " normal chairs and " + random.Next(min, max) +" chairs for babies. How many chairs does the restaurant have in total?",
        "How many cookies did you sell if you sold " + random.Next(min, max) + " chocolate cookies and " + random.Next(min, max) + " vanilla cookies?",
        girl2names[GirlName2] + " bought a big bag of candy. The bag had " + random.Next(min, max) + " blue candies, " + random.Next(min, max) + " red candies and " + random.Next(min, max) + " green candies. How many candies were there in total?",
        girl1names[GirlName] + " had " + random.Next(min, max) + " books at home. He went to the library to take out " + random.Next(min, max) + " more books. He then bought 1 book. How many books does Billy have now?",
        "The hobby store normally sells " + random.Next(min, max) + " trading cards per month. In June, the hobby store sold " + random.Next(min, max) + " more trading cards than normal. In total, how many trading cards did the hobby store sell in June?",
        girl2names[GirlName2] + " has " + random.Next(min, max) + " pieces of gum to share with her friends. There wasn’t enough gum for all her friends, so she went to the store to get " + random.Next(min, max) + " more pieces of gum. How many pieces of gum does Adrianna have now?",
        girl1names[GirlName] + " has " + random.Next(min, max) + " pieces of gum to share with her friends. There wasn’t enough gum for all her friends, so she went to the store and got " + random.Next(min, max) + " pieces of strawberry gum and " + random.Next(min, max) + " pieces of bubble gum. How many pieces of gum does Adrianna have now?"
    };
    genericfunction(list);
    //int index = randoms.Next(list.Count);

    //DisplayAlert("word problems", list[index], "ok");

    //Creates a new Word document 
    WordDocument wordDocument = new WordDocument();

    //Adds new section with single paragraph to the document
    wordDocument.EnsureMinimal();
    wordDocument.LastSection.PageSetup.Margins.All = 15;
    //Get Last paragraph of the document
    IWParagraph paragraph = wordDocument.LastParagraph;
    //Create a custom style
    WParagraphStyle paragraphStyle = wordDocument.Styles.FindByName("Normal") as WParagraphStyle;
    paragraphStyle.CharacterFormat.Font = new Syncfusion.Drawing.Font("Times New Roman", 20);
    paragraphStyle.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Justify;
    paragraphStyle.ParagraphFormat.AfterSpacing = 15f;
    WSection section = wordDocument.LastSection;
    for (int i = 0; i < wordproblemslist.Count; i++)
    {
        paragraph = section.AddParagraph();
        string t = wordproblemslist[i];
        paragraph.AppendText(t);
    }

    //Adds new text to the paragraph
    //paragraph.AppendText(text);
    //Adds first text to the paragraph
    //  paragraph = section.AddParagraph();
    // paragraph.AppendText(text);
    //Second paragraph
    //  paragraph = section.AddParagraph();
    //  paragraph.AppendText(text);
    //Instantiation of DocIORenderer for Word to PDF conversion
    DocIORenderer render = new DocIORenderer();
    //Converts Word document into PDF document
    PdfDocument pdfDocument = render.ConvertToPDF(wordDocument);
    //Releases all resources used by the Word document and DocIO Renderer objects
    render.Dispose();
    wordDocument.Dispose();
    //Save the document into memory stream
    MemoryStream stream = new MemoryStream();
    pdfDocument.Save(stream);
    stream.Position = 0;
    //Close the document 
    pdfDocument.Close();
    //Save the stream into pdf file
    //The operation in Save under Xamarin varies between Windows Phone, Android and iOS platforms. Please refer PDF/Xamarin section for respective code samples.

    Xamarin.Forms.DependencyService.Get<ISave>().SaveAndView("Output.pdf", "application/pdf", stream);

    wordproblemslist.Clear();
    girl1names.Clear();
    girl2names.Clear();
    private void genericfunction( List<string> list)
    {
        var random = new Random();
        List<int> listNumbers = new List<int>();

        int number;
        for (int j = 0; j < questions; j++)
        {
            do
            {
                number = randoms.Next(list.Count);
            } while (listNumbers.Contains(number));
            listNumbers.Add(number);
        }
        for (int k = 0; k < listNumbers.Count; k++)
        {
            wordproblemslist.Add(list[listNumbers[k]]);
        }
    }
4

2 回答 2

0

您正在以GirlName这种方式初始化:

GirlName = random.Next(girlsLIST1.Count);

所以在这里,GirlName将介于0和之间5

那么,您正试图通过这种方式获得名称girl1names

girl1names[GirlName]

但是girl1names仅包含questions名称,因为您以这种方式填写了此列表:

for (int i = 0; i < questions; i++)
{
    girl1names.Add(girlsLIST1[GirlName]);
}

因此,如果发生GirlName大于或等于 的情况questions,您就出局了。

(同样适用girl2names[GirlName2]


解决方案

不要尝试访问girl1names[GirlName].

使用不会超过girl1names's 长度的索引(并且可能重新考虑您真正想要达到的索引,因为GirlName不太可能是您正在寻找的索引)

girl2names.

于 2019-04-11T10:34:40.927 回答
0

原因:

你会得到一个异常,这Index Was Out Of Range. Must Be Non Negative And Less Then The Size Of Collection意味着你想要获取的对象的索引超出了范围。

它发生在这里:girl1names[GirlName]girl2names[GirlName2]

girl1names: 这是一个包含名字的列表。它的计数取决于问题value的数量。

GirlName: 在Index这里工作。它的最大值取决于GirlsLIST1.Count

为防止出现异常,应确保 的值GirlName小于 length of girl1names

因此,这意味着您定义的GirlsLIST1.Count的值应该小于questions。(与 相同girl2names[GirlName2]

我猜你的 question 值小于 4,所以如果你将 5 个字符串值增加到 6,它会崩溃。

解决方案:

确保问题的值大于GirlsLIST1.CountGirlsLIST2.Count

于 2019-04-12T06:44:22.687 回答