Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Fórum ➡ Building a List<> with questions and answers, from data in TXT file, using LINQ ♦
//Rextester.Program.Main is the entry point for your code. Don't change it. //Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5 using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; // aljodav ➡ rextester.comm/users/376 // in reply to the MSDN thread ➡ http://bit.ly/2owfxcj namespace Rextester { using Tp=Tuple<string,List<string>>; public class Program { public static string[] FileReadAllLines() { return new string[]{ ""," ","*This is the Question 1","Yes","No", "*Question 2",""," ","Definitely YES!","Sometimes yes","No", "*This is the Question 3","What? Could you repeat, please?", "*Question 4","I don't know","I wish I knew it","I don't care about it", "*This is the Question 5","Just 1","2, maybe 3","4 or more" }; } public static void Main(string[] args) { List<Tp> tps=FileReadAllLines() // in lieu of File.ReadAllLines(string path) .Where(s=>false==string.IsNullOrWhiteSpace(s)) .Aggregate(new List<Tp>(),(l,s)=>{ if(s[0]=='*'){ Tp tp=new Tp(s.Substring(1),new List<string>()); l.Add(tp); }else{ l[l.Count-1].Item2.Add(s); } return l; }) ; /* // all questions and answers: tps.ToList().ForEach(t=>{ Console.WriteLine(t.Item1); t.Item2.ForEach(s=>Console.WriteLine('\t'+s)); }); */ Console.WriteLine("A randomly chosen question:"); Tp tup=tps[new Random().Next(tps.Count)]; Console.WriteLine("\nThe question:\n\t{0}\n\nThe options:",tup.Item1); tup.Item2.ForEach(s=>Console.WriteLine('\t'+s)); Console.WriteLine("\n\nHello, world!"); } } }
run
|
edit
|
history
|
help
0
NumberSeenSample
checkvalidtraingle
Gerador de Matriz Data
Delegates
pass-len-to-predicate-modified
rzError
a6
Bridge Design Patern
FREE V-BUCKS GENERATOR NO HUMAN VERIFICATION 2022 [zT4]
printing * in pyramid shape