|
-
Dec 3rd, 2005, 01:31 AM
#1
Thread Starter
Hyperactive Member
Lottery Program
Code:
/* Make a lottery program
Picks 7 random numbers
Asks the user for 7 numbers
Checks how many match
they can be in any order, like the first number the user chooses can match the 4th random number
And all the random numbers have to be different
*/
using System;
namespace Lottery
{
class Test
{
static void Main()
{
Console.Write("Welcome To Lottery, would you like to play? (yes/no) ");
string answer = Console.ReadLine();
if (answer == "yes") {
Console.WriteLine("Selecting Numbers at Random...");
Random rand = new Random();
int [] number;
number = new int[6];
for( int i = 0; i < 7; i++ )
{
number[i] = rand.Next(1, 49);
}
Console.WriteLine("Numbers have been selected!");
}
else {
Console.WriteLine("Thanks Anyways!");
Console.ReadLine();
}
}
}
}
That's what i have so far.
I am trying to check if any of the numbers randomly generated are duplicates and then replace them if they are. However, i have been unableto accomplish this, so any help would be appreciated.
If you have any suggestions on how to improve on my code that would be very helpfull.
Thank You
- Joel
Last edited by BaDDBLooD; Dec 3rd, 2005 at 01:34 AM.
-
Dec 3rd, 2005, 01:38 AM
#2
Re: Lottery Program
you need to add some checking inside your for loop.i.e,
Code:
bool numberclear = false;
while (numberclear=false)
{
number[i] = rand.Next(1, 49);
int x = number[i];
for (int j=0;j<i;j++)
{
if number[j]=x numberclear=false;
else numberclear = false;
}
}
//Now the number shouldn't be like the random one.
Or something to that effect, as I'm on my way out the door..
Bill
-
Dec 3rd, 2005, 02:04 AM
#3
Thread Starter
Hyperactive Member
Re: Lottery Program
I changed my code to
Code:
/* Make a lottery program
Pick 7 random numbers
Ask the user for 7 numbers
Check how many match
they can be in any order, like the first number the user chooses can match the 4th random number
All the random numbers have to be different
*/
using System;
namespace Lottery
{
class Test
{
static void Main()
{
Console.Write("Welcome To Lottery, would you like to play? (yes/no) ");
string answer = Console.ReadLine();
if (answer == "yes") {
Console.WriteLine("Selecting Numbers at Random...");
Random rand = new Random();
int [] number;
number = new int[6];
bool duplicate = false;
for( int i = 0; i < 7; i++ )
{
number[i] = rand.Next(1, 49);
for (int j= 0; j < i; j++)
{
do
{
if (number[j] == number[i])
{
number[i] = rand.Next(1, 49);
duplicate = true;
}
else
{
duplicate = false;
}
} while (duplicate == true);
}
Console.WriteLine(number[i]);
}
Console.WriteLine("Numbers have been selected!");
}
else {
Console.WriteLine("Thanks Anyways!");
Console.ReadLine();
}
}
}
}
I get an exception now, and i really don't see what i am doing wrong.
Last edited by BaDDBLooD; Dec 3rd, 2005 at 02:11 AM.
-
Dec 3rd, 2005, 02:52 AM
#4
Re: Lottery Program
I suggest that you don't just select a number at random and then check that it's not a duplicate. You can ensure that you don't choose a duplicate in the first place by removing the numbers already selected from the pool:
Code:
int[] numbers = new int[48];
for (int i = 0; i < numbers.Length; i++)
{
numbers[i] = i + 1;
}
ArrayList numberList = new ArrayList();
numberList.AddRange(numbers);
Random numberGenerator = new Random();
int[] selectedNumbers = new int[7];
int selectedIndex;
for (int i = 0; i < selectedNumbers.Length; i++)
{
selectedIndex = numberGenerator.Next(0, numberList.Count);
selectedNumbers[i] = (int)numberList[selectedIndex];
numberList.RemoveAt(selectedIndex);
}
-
Dec 3rd, 2005, 03:10 AM
#5
Thread Starter
Hyperactive Member
Re: Lottery Program
Thank you
Two questions
1) Can't you just add directly to the array list instead of adding a "Range"
2) Why do you need (int) in the following statement
Code:
(int)numberList[selectedIndex];
-
Dec 3rd, 2005, 03:23 AM
#6
Re: Lottery Program
1) Yes, you could just use a "for" loop and Add the numbers from 1 to 48 directly to the ArrayList.
2) An ArrayList stores Object references, so when you retrieve an item from an ArrayList you get it as an Object. In order to assign it to an "int" variable you must cast it as an "int" first.
-
Dec 3rd, 2005, 03:24 AM
#7
Thread Starter
Hyperactive Member
Re: Lottery Program
is it better to add a range or to add them directly?
-
Dec 3rd, 2005, 03:41 AM
#8
Re: Lottery Program
Much of a muchness really. It would almost certainly be more efficient to use AddRange, but for such a small number of items it would make such a small difference I wouldn't worry about it.
-
Dec 3rd, 2005, 03:48 AM
#9
Thread Starter
Hyperactive Member
Re: Lottery Program
Well thank you again, i am glad i know how to use Array Lists now
-
Dec 3rd, 2005, 06:34 PM
#10
Thread Starter
Hyperactive Member
Re: Lottery Program
Another question
Why did you have
Code:
int[] numbers = new int[48];
for (int i = 0; i < numbers.Length; i++)
{
numbers[i] = i + 1;
}
ArrayList numberList = new ArrayList();
numberList.AddRange(numbers);
Random numberGenerator = new Random();
int[] selectedNumbers = new int[7];
int selectedIndex;
for (int i = 0; i < selectedNumbers.Length; i++)
{
selectedIndex = numberGenerator.Next(0, numberList.Count);
selectedNumbers[i] = (int)numberList[selectedIndex];
numberList.RemoveAt(selectedIndex);
}
-
Dec 3rd, 2005, 06:41 PM
#11
Re: Lottery Program
I assume that you're asking why I used "i" on one side and "i + 1" on the other. You want the array to contain the numbers 1 to 48, but arrays are zero-based. This means that the value at index 0 should be 1, the value at index 1 should be 2, the value at index 2 should be 3 and so on. As you see, the value at each index is one greater than the index itself.
-
Dec 3rd, 2005, 06:42 PM
#12
Thread Starter
Hyperactive Member
Re: Lottery Program
 Originally Posted by jmcilhinney
I assume that you're asking why I used "i" on one side and "i + 1" on the other. You want the array to contain the numbers 1 to 48, but arrays are zero-based. This means that the value at index 0 should be 1, the value at index 1 should be 2, the value at index 2 should be 3 and so on. As you see, the value at each index is one greater than the index itself.
Again, many thanks!
-
Dec 4th, 2005, 03:23 AM
#13
Thread Starter
Hyperactive Member
Re: Lottery Program
Code:
/* Make a lottery program
Pick 7 random numbers
Ask the user for 7 numbers
Check how many match
they can be in any order, like the first number the user chooses can match the 4th random number
All the random numbers have to be different
*/
using System;
using System.Collections;
namespace Lottery
{
class Test
{
static void Main()
{
Console.WriteLine("Welcome To Lottery!");
Console.WriteLine("Please Enter Your Numbers!");
ArrayList Number = new ArrayList();
for (int i = 0; i < 7; i++)
{
Console.Write("Number #{0} ", i + 1);
Number.Add(Console.ReadLine());
}
Console.WriteLine("You Have Chosen The Numbers {0}, {1}, {2}, {3}, {4}, {5}, {6}", Number[0], Number[1], Number[2], Number[3], Number[4], Number[5], Number[6]);
Console.WriteLine("Mixing Balls...");
int[] Balls = new int[49];
for (int j = 0; j < Balls.Length; j++)
{
Balls[j] = j + 1;
}
ArrayList Lotto = new ArrayList();
Lotto.AddRange(Balls);
Random Generator = new Random();
int[] Selected = new int[7];
int Ball;
for (int k = 0; k < Selected.Length; k++)
{
Ball = Generator.Next(0, Lotto.Count);
Selected[k] = (int)Lotto[Ball];
Lotto.RemoveAt(Ball);
if (Number.Contains(Selected[k]) == true)
{
Console.WriteLine("Your Number {0} Is Correct!", Number[k]);
}
else
{
Console.WriteLine("The #{0} Ball Has Been Chosen!", Selected[k]);
}
}
Console.ReadLine();
}
}
}
Apparently, it can't compare them to see if the ball has been called or not.
Any help would be greatly appreciated!
Would it have something to do with Contains wanting a Object?
- Joel
-
Dec 4th, 2005, 04:44 AM
#14
Re: Lottery Program
That's not the issue. I don't have time to review your code at the moment I'm afraid. If the problem is unresolved when I get back to my machine tomorrow morning I'll take a closer look.
-
Dec 4th, 2005, 12:01 PM
#15
Fanatic Member
Re: Lottery Program
ArrayList.Contains uses Object.Equals to compare. This means that it looks for the same instance not the same value.
Maybe BinarySearch will do the trick.
"so just keep in mind that fantasy is not the same as realtiy and make sure u remember that wii sports may be fun but u cant count on it as exercise ok cool bye" - HungarianHuman
-
Dec 4th, 2005, 03:14 PM
#16
Thread Starter
Hyperactive Member
Re: Lottery Program
Code:
/* Make a lottery program
Pick 7 random numbers
Ask the user for 7 numbers
Check how many match
they can be in any order, like the first number the user chooses can match the 4th random number
All the random numbers have to be different
*/
using System;
using System.Collections;
namespace Lottery
{
class Test
{
static void Main()
{
Console.WriteLine("Welcome To Lottery!");
Console.WriteLine("Please Enter Your Numbers!");
ArrayList Number = new ArrayList();
for (int i = 0; i < 7; i++)
{
Console.Write("Number #{0} ", i + 1);
Number.Add(Console.ReadLine());
}
Console.WriteLine("You Have Chosen The Following Numbers:");
Print(Number);
Console.WriteLine("Mixing Balls...");
int[] Balls = new int[49];
for (int j = 0; j < Balls.Length; j++)
{
Balls[j] = j + 1;
}
ArrayList Lotto = new ArrayList();
Lotto.AddRange(Balls);
Random Generator = new Random();
ArrayList Selected = new ArrayList();
int Ball;
for (int k = 0; k < 7; k++)
{
Ball = Generator.Next(0, Lotto.Count);
Selected.Add(Lotto[Ball]);
Lotto.RemoveAt(Ball);
}
Console.WriteLine("The Current Balls are:");
Print(Selected);
for (int x = 0; x < Selected.Count; x++)
{
Compare(Number, Selected[x]);
}
Console.ReadLine();
}
public static void Print(IEnumerable List)
{
IEnumerator Enumerator = List.GetEnumerator();
while (Enumerator.MoveNext())
{
Console.Write( "\t{0}", Enumerator.Current);
}
Console.WriteLine();
}
public static void Compare(ArrayList List1, Object Item)
{
int Index = List1.BinarySearch(Item);
if ( Index < 0 )
{
Console.WriteLine("Ball #{0} Does Not Match Any Of Your Numbers!", Item);
}
else
{
Console.WriteLine( "Ball #{0} Matches Number {2}!", Item, List1[Index]);
}
}
}
}
I am getting the error:
An unhandled exception of type 'System.InvalidOperationException' occurred in mscorlib.dll
Additional Information: Specified IComparer Threw an exception.
Not sure what's wrong, i've tried a few things to fix the error, but to no avail.
Thank You Guys so Much
Last edited by BaDDBLooD; Dec 4th, 2005 at 03:19 PM.
-
Dec 4th, 2005, 05:05 PM
#17
Re: Lottery Program
 Originally Posted by grilkip
ArrayList.Contains uses Object.Equals to compare. This means that it looks for the same instance not the same value.
Maybe BinarySearch will do the trick.
You are quite correct that ArrayList.Contains calls Object.Equals, however here's a quote from the help topic for that method:
For reference types, equality is defined as object equality; that is, whether the references refer to the same object. For value types, equality is defined as bitwise equality.
This means that if the ArrayList contains Integers then Contains will check for the existence of a value, not an instance.
-
Dec 5th, 2005, 01:17 AM
#18
Thread Starter
Hyperactive Member
Re: Lottery Program
 Originally Posted by jmcilhinney
You are quite correct that ArrayList.Contains calls Object.Equals, however here's a quote from the help topic for that method:This means that if the ArrayList contains Integers then Contains will check for the existence of a value, not an instance.
So does that mean it will return a boolean value if the value is found inside the array list?
-
Dec 5th, 2005, 01:34 AM
#19
Re: Lottery Program
Code:
ArrayList al = new ArrayList;
al.Add(1);
al.Add(2);
al.Add(3);
// This line will display True in a MessageBox.
MessageBox.Show(al.Contains(2).ToString());
-
Dec 5th, 2005, 02:44 AM
#20
Thread Starter
Hyperactive Member
Re: Lottery Program
i am trying to make a functin to see how many balls are correct
Code:
public static int Find(ArrayList Numbers, ArrayList Balls)
{
int matching;
for (int i = 0; i < Numbers.Count; i++)
{
if (Balls.Contains(Numbers[i]) == true)
{
Console.WriteLine("Number {0} Matches!", Numbers[i]);
matching++;
}
return matching;
}
}
it won't let me return a integer value, but i am not really sure on how to create a function to return a value, did i do it right?
EDIT:
i supposed i'd have to do this wouldn't i?
Code:
if (Find(Numbers, Selected).ToString == 7)
{
Console.WriteLine("You Have WON!");
}
else
{
Console.WriteLine("You Did Not Win, Would You Like To Try Again?");
}
Last edited by BaDDBLooD; Dec 5th, 2005 at 02:48 AM.
-
Dec 5th, 2005, 05:07 PM
#21
Thread Starter
Hyperactive Member
Re: Lottery Program
For some reason my Function Name "Find" is underlined
and i am getting the error
'Lottery.Test.Find(System.Collections.ArrayList, System.Collections.ArrayList)': not all code paths return a value
-
Dec 5th, 2005, 05:18 PM
#22
Re: Lottery Program
That means that it is possible for your code to execute in such a way that it reaches the end of the function without encountering a "return" statement. It would be because your only "return" statement is inside the "for" loop. If Numbers.Count is zero then the code in the "for" loop doesn't get executed and that return statement never gets executed. For this reason, many people recommend having a single "return" statement at the end of the function that returns the value of a local variable, which you would have set at the appropriate points earlier in your code.
-
Dec 5th, 2005, 05:36 PM
#23
Thread Starter
Hyperactive Member
Re: Lottery Program
Thank you so much 
Unfortunately i came into a really stupid problem and i have NO Idea, i am totally dumb founded.
Code:
public static int Find(ArrayList Numbers, ArrayList Balls)
{
int count;
for (int i = 0; i < Numbers.Count; i++)
{
if (Balls.Contains(Numbers[i]) == true)
{
Console.WriteLine("Number {0} Matches!", Numbers[i]);
count++;
}
}
return count;
}
Error: Use of unassigned local variable 'count'
-
Dec 5th, 2005, 05:50 PM
#24
Re: Lottery Program
In C# you have to assign a value to a variable before you can use it as a method argument or on the right-hand side of an assignment. "count++" implies that you want to add 1 to the value of count, but you have not assigned a value to count to add 1 to. If you want count to have the value zero then you need to assign it explicitly, i.e.:
-
Dec 5th, 2005, 06:11 PM
#25
Thread Starter
Hyperactive Member
Re: Lottery Program
Code:
/* Make a lottery program
Pick 7 random numbers
Ask the user for 7 numbers
Check how many match
they can be in any order, like the first number the user chooses can match the 4th random number
All the random numbers have to be different
*/
using System;
using System.Collections;
namespace Lottery
{
class Test
{
static void Main()
{
Console.WriteLine("Welcome To Lottery!");
Console.WriteLine("Please Enter Your Numbers!");
ArrayList Numbers = new ArrayList();
for (int i = 0; i < 7; i++)
{
Console.Write("Number #{0} ", i + 1);
Numbers.Add(Console.ReadLine());
}
Console.WriteLine("You Have Chosen The Following Numbers:");
Print(Numbers);
Console.WriteLine("Mixing Balls...");
int[] Balls = new int[49];
for (int j = 0; j < Balls.Length; j++)
{
Balls[j] = j + 1;
}
ArrayList Lotto = new ArrayList();
Lotto.AddRange(Balls);
Random Generator = new Random();
ArrayList Selected = new ArrayList();
int Ball;
for (int k = 0; k < 7; k++)
{
Ball = Generator.Next(0, Lotto.Count);
Selected.Add(Lotto[Ball]);
Lotto.RemoveAt(Ball);
}
Console.WriteLine("The Current Balls are:");
Print(Selected);
if (Find(Numbers, Selected) == 7)
{
Console.WriteLine("You Have WON!");
}
else
{
Console.WriteLine("You Did Not Win, Would You Like To Try Again?");
}
Console.ReadLine();
}
public static void Print(IEnumerable List)
{
IEnumerator Enumerator = List.GetEnumerator();
while (Enumerator.MoveNext())
{
Console.Write( "\t{0}", Enumerator.Current);
}
Console.WriteLine();
}
public static int Find(ArrayList Numbers, ArrayList Balls)
{
int count = 0;
for (int i = 0; i < Numbers.Count; i++)
{
if (Balls.Contains(Convert.ToInt32(Numbers[i])) == true)
{
Console.WriteLine("Number {0} Matches!", Numbers[i]);
count++;
}
}
return count;
}
}
}
K i got it all working.
However one more question, it isn't a problem though.
Why does it have to be a int/int32, and not a short/int16.. or even a byte?
-
Dec 5th, 2005, 06:18 PM
#26
Re: Lottery Program
Why does what have to be Int32? There is not really any reason to use integral types other than Int32 in the general case. You might think that you're making your app more efficient by using an Int16 in situations where you don't need values any greater than that type provides, but when working on a 32-bit machine there is a certain amount of translation involved when working with 16-bit numbers, so what you gain in memory usage you lose in execution speed.
-
Dec 5th, 2005, 06:22 PM
#27
Thread Starter
Hyperactive Member
Re: Lottery Program
Thank you for clearing that up.
I wanted to experiment with making my project more Object Oriented, using like more then one class, inheriting other classes, shared functions, delegates and what not.
Have any ideas how i can experiment with this and still have my game work?
This is all just one big learning experience for me and it's going pretty well so far 
Thank You Once Again
- Joel
-
Dec 5th, 2005, 10:23 PM
#28
Thread Starter
Hyperactive Member
Re: Lottery Program
Code:
Console.WriteLine("Would You Like To Play Again? (yes/no) ");
string Answer = Console.ReadLine();
} while (Answer == "yes" || Answer == "no");
I get the error: The name 'Answer' does not exist in the class or namespace 'Lottery.Test'
I declared it.. so i don't see what i did.
-
Dec 5th, 2005, 10:35 PM
#29
Re: Lottery Program
You've declared it within the braces so it doesn't exist outside. It is local just to that block.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|