| | |
Loop Madness!
Please support our ASP.NET advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jan 2009
Posts: 3
Reputation:
Solved Threads: 0
having some real difficulties with a loop I have that checks lottery numbers against users' selected numbers stored in a database. At the moment, the loop (below) will get all the users who have 1 ball that matches the lottery, but I don't know how to store this and keep cycling so that I only display users with 5 balls or 6 balls :-(. here is my loop so far:
the ball1 etc are the numbers of the lottery and usersball1 etc are the users numbers in the database.
As I mentioned, this successfully outputs the users with 1 matching ball. I want to output users with 5 balls or over ideally. Any ideas how I can go about this?
thanks a LOT!
ASP.NET Syntax (Toggle Plain Text)
if (ball1 == usersball1 || ball2 == usersball1 || ball3 == usersball1 || ball4 == usersball1 || ball5 == usersball1 || ball6 == usersball1 || ball1 == usersball2 || ball2 == usersball2 || ball3 == usersball2 || ball4 == usersball2 || ball5 == usersball2 || ball6 == usersball2 || ball1 == usersball3 || ball2 == usersball3 || ball3 == usersball3 || ball4 == usersball3 || ball5 == usersball3 || ball6 == usersball3 || ball1 == usersball4 || ball2 == usersball4 || ball3 == usersball4 || ball4 == usersball4 || ball5 == usersball4 || ball6 == usersball4 || ball1 == usersball5 || ball2 == usersball5 || ball3 == usersball5 || ball4 == usersball5 || ball5 == usersball5 || ball6 == usersball5 || ball1 == usersball6 || ball2 == usersball6 || ball3 == usersball6 || ball4 == usersball6 || ball5 == usersball6 || ball6 == usersball6) { lblResult.Visible = true; lblResult.Text = lblResult.Text + "flag: " + System.Convert.ToString(flag) + lblOthers.Text + System.Convert.ToString(row2["UserName"]) + " matched at least 1 ball!<br />"; }
the ball1 etc are the numbers of the lottery and usersball1 etc are the users numbers in the database.
As I mentioned, this successfully outputs the users with 1 matching ball. I want to output users with 5 balls or over ideally. Any ideas how I can go about this?
thanks a LOT!
hey although the way your writing this can be done, it will be hard to maintain with all your permutations in the if statement. I have put together an example for you that uses lists to iterate through and uses built in Array methods to find out how many each person has.
the above code uses generic collections to hold the LotteryTicket object, these can be very powerful when used properly, but the simple use of the Array method 'Contains' does the check you were performing before.
ASP.NET Syntax (Toggle Plain Text)
public void StartCheck() { /** Holds the lottery results, see the LotteryTicket class at the bottom **/ LotteryTicket results = new LotteryTicket("The Lotto Results", 5, 12, 15, 9, 34, 23); /** Create a List of type LotteryTicket to hold each users results, you could populate this using a database **/ List<LotteryTicket> listToCheck = new List<LotteryTicket>(); listToCheck.Add(new LotteryTicket("Alan", 1, 2, 3, 4, 5, 6)); listToCheck.Add(new LotteryTicket("John", 59, 58, 57, 56, 55, 54)); listToCheck.Add(new LotteryTicket("Cheryl", 9, 4, 34, 1, 2, 23)); /** Iterate through each ticket **/ foreach(LotteryTicket ticket in listToCheck) { /** Counts the number of matches **/ int numberOfMatches = 0; foreach (int number in ticket.Balls) { /** Compares the lottery results with the persons lottery ticket **/ if (results.Balls.Contains(number)) { /** If a match is found the number is incremented **/ numberOfMatches++; } } /** Outputs a message to the console, you can change this to display your message to the web page **/ Console.Out.WriteLine("{0} has matched {1} lottery numbers.", ticket.Name, numberOfMatches); } } /** Class to model a lottery ticket and the person who owns it **/ private class LotteryTicket { /** The lottery tickets balls **/ private int[] balls; /** The persons name **/ private string name; /** Constructor, pass through the persons names and the balls selected **/ public LotteryTicket(string name, int ball01, int ball02, int ball03, int ball04, int ball05, int ball06) { this.name = name; this.balls = new int[] {ball01, ball02, ball03, ball03, ball04, ball05, ball06}; } /** Property to get the balls **/ public int[] Balls { get { return this.balls; } } /** Property to get the name **/ public string Name { get { return this.name; } } }
the above code uses generic collections to hold the LotteryTicket object, these can be very powerful when used properly, but the simple use of the Array method 'Contains' does the check you were performing before.
Last edited by Fungus1487; Jan 9th, 2009 at 6:07 pm.
When Autumn Falls [ http://www.whenautumnfalls.co.uk ] &&
Designdotworks [ http://www.designdotworks.co.uk ] Web / Graphic / Software Design
Designdotworks [ http://www.designdotworks.co.uk ] Web / Graphic / Software Design
![]() |
Similar Threads
- print multi-dimensional array (Assembly)
- python calulator help (Python)
- March Madness Program Help (Visual Basic 4 / 5 / 6)
Other Threads in the ASP.NET Forum
- Previous Thread: C# XMLDataSource - filtering XML
- Next Thread: Open Page2 from Page1 in Web Developer
| Thread Tools | Search this Thread |
.net 2.0 3.5 ajax appliances application asp asp.net beginner box browser businesslogiclayer button c# c#gridviewcolumn cac checkbox child class compatible confirmationcodegeneration content contenttype control countryselector courier css database datagrid datagridview datalist deadlock deployment development dgv dialog dropdownmenu dynamic dynamically edit embeddingactivexcontrol feedback fileuploader fill findcontrol flash flv form forms grid gridview gudi homeedition hosting iis image javascript jquery list menu mssql multistepregistration nameisnotdeclared novell objects opera order problem ratings redirect registration relationaldatabases rotatepage search security select serializesmo.table sessionvariables silverlight smoobjects sql ssl tracking treeview typeof validatedate validation vb.net virtualdirectory vista visual-studio visualstudio vs2008 web webapplications webarchitecture webdevelopment wizard xml xsl





