Reputation: 37
I'm new to C# we have an activity to create a lottery game.
1 matching number won $10 2 matching number won $100 3 matching number not in order $1,000 3 matching number in order won $10,000
I'm having issues with my code even there are 2 matching or 3 matching number it always display $10. Any help would be appreciated.
Below are the source code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LotteryGame
{
class Program
{
static void Main(string[] args)
{
// Matching numbers awards
int rNumMatchOne = 10;
int rNumMatchTwo = 100;
int rNumMatchThree = 1000;
int rNumMatchFour = 10000;
// Generate random numbers
Random randomNum = new Random();
// Integers Declaration
int rNum1;
int rNum2;
int rNum3;
int rNumIput;
int guessNum;
// Arrays Declartion
int[] guessNumMatch = new int[3];
int[] guessNumSort = new int[3];
int[] guessInput = new int[3];
// Restrict inputs between 1 and 4 only
rNum1 = randomNum.Next(1, 5);
rNum2 = randomNum.Next(1, 5);
rNum3 = randomNum.Next(1, 5);
Console.Write("C# Lottery Game\n\n");
Array.Sort(guessNumSort); // sort random numbers
// Guess number input loop
for (rNumIput = 0; rNumIput < 3; rNumIput++)
{
Console.Write("Guess Number " + (rNumIput + 1) + ": ");
guessNum = Convert.ToInt32(Console.ReadLine());
// Invalid input between 1 and 4 program will loop back and enter correct number
while (guessNum < 1 || guessNum > 4)
{
Console.WriteLine("\n");
Console.WriteLine("Invalid Number. Please enter number between 1 and 4. \n");
Console.Write("Guess Number " + (rNumIput + 1) + ": ");
guessNum = Convert.ToInt32(Console.ReadLine());
}
guessNumMatch[rNumIput] = guessNum;
guessNumSort[rNumIput] = guessNum;
}
Array.Sort(guessNumSort);
// Display random numbers and entered numbers
Console.WriteLine();
Console.WriteLine("Generated random numbers are : " + rNum1 + " | " + rNum2 + " | " + rNum3);
Console.WriteLine("Numbers you entered are : " + guessNumMatch[0] + " | " + guessNumMatch[1] + " | " + guessNumMatch[2]);
// Matching 1 number
if (guessNumMatch[0] == rNum1 || guessNumMatch[1] == rNum2 || guessNumMatch[2] == rNum3)
{
Console.WriteLine("\n");
Console.WriteLine("CONGRATULATIONS! YOU WON: $" + rNumMatchOne);
}
// Matching 2 numbers
else if ((guessNumMatch[0] == rNum1 && guessNumMatch[1] == rNum2) || (guessNumMatch[1] == rNum2 && guessNumMatch[2] == rNum3))
{
Console.WriteLine("\n");
Console.WriteLine("CONGRATULATIONS! YOU WON: $" + rNumMatchTwo);
}
// Matching 3 numbers not in order
else if (guessNumSort[0] == guessInput[0] && guessNumSort[1] == guessInput[1] && guessNumSort[2] == guessInput[2])
{
Console.WriteLine("\n");
Console.WriteLine("CONGRATULATIONS! YOU WON: $" + rNumMatchThree);
}
// Matching 3 numbers exact order
else if (guessNumMatch[0] == rNum1 && guessNumMatch[1] == rNum2 && guessNumMatch[2] == rNum3)
{
Console.WriteLine("\n");
Console.WriteLine("CONGRATULATIONS! YOU WON: $" + rNumMatchFour);
}
else // No matching numbers
{
Console.WriteLine("\n");
Console.WriteLine("SORRY, NO MATCHING NUMBERS FOUND! ");
}
Console.WriteLine("\n");
Console.WriteLine("PRESS ANY KEY TO EXIT PROGRAM ");
Console.ReadKey();
}
}
}
Upvotes: 2
Views: 1208
Reputation: 682
Your first if
statement will evaluate to true if at least one of the numbers is correct. For example, if the user guess the 2nd and 3rd number correctly, guessNumMatch[1] == rNum2
will evaluate to true
. if(false || true || true)
evaluates to true
so the statement gets executed. The other if
statements will be skipped.
One solution (as Attersson beat me to) is to invert your if statements - check if all 3 are true, then if 2 are true, etc.
Upvotes: 0
Reputation: 4866
Invert the order of your if statements. Check first if 3 numbers are matched in order, then 3 then 2 then 1 and last no match.
Otherwise the first if statement hits true even when there is more than 1 match.
Upvotes: 5