我有两个字符串列表(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]]);
}
}