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();
}
}
}
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.
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.
Related
I've created a program that adds all input values from the user and prints the sum if the user entered 0 or greater than 101. Here's my code:
int n, sum = 0;
do
{
Console.Write("Enter a number:");
n = int.Parse(Console.ReadLine());
sum += n;
}
while (n != 0 && n < 101);
Console.WriteLine("Sum is:" + sum);
Console.ReadKey();
I'm trying to figure how to accepts numbers alternately. For example, Input values are: 4, 7, 8, 3, 6, 1. If the user input two consecutive odd or even number the system will not accept two consecutive odd or even or it will display the sum of all inputted numbers.
Taking the recomendation of Andrew an Peter you can add the list to save your previous inputs and do some checks to the current and prev data to do the logic.
the code that implements this is the following:
//save input list
List<int> inputNumbers = new List<int>();
int n, sum = 0;
do
{
Console.Write("Enter a number:");
n = int.Parse(Console.ReadLine());
//My Recomendation ==============================
//save previus input numbers
inputNumbers.Add(n);
//Check is there are previous input number
if(inputNumbers.Count>1){
//New control vars
Boolean previousNumberIsOdd= false,currentNumberIsOdd= false;
foreach (int item in inputNumbers)
{
Console.Write(item+",");
}
//check if previus number is odd
if((inputNumbers[inputNumbers.Count-2])%2 == 0){
previousNumberIsOdd = true;
}
//Check if current number is odd
if(n%2==0){
currentNumberIsOdd = true;
}
Console.WriteLine("Control vars:" + previousNumberIsOdd +"/"+currentNumberIsOdd);
//Check diferent scenarios and do the logic
//previous and current number are odds
if(previousNumberIsOdd && currentNumberIsOdd){
//break while and write the result
break;
}
//previous and current number are evens
if(!previousNumberIsOdd && !currentNumberIsOdd){
//break while and write the result
break;
}
}
//if there aren't numbers to break the cycle then do the original logic
//End of my recomendation =====================
sum += n;
}
while (n != 0 && n < 101);
Console.WriteLine("Sum is:" + sum);
Console.ReadKey();
}
You can do many optimizations to this code but a put it like that to be very clear in the logic.
By using two flag you can achieve the result.
int n, sum = 0;
bool previous = false, current;
bool init = true;
do
{
Console.Write("Enter a number:");
n = int.Parse(Console.ReadLine());
current = n % 2 == 0;
if( current == previous && !init)
break;
previous = current;
init = false;
sum += n;
}
while (n != 0 && n < 101);
Console.WriteLine("Sum is:" + sum);
Console.ReadKey();
Output
I am a beginner in C# programming and trying to code a method that ask the user to give a start and an end integer number, then sum up all numbers from the start to the end and in case the given start number is greater than the end number, swap the values so that the start number becomes the end number and the end number gets the value of the start number.
This what I have done so far but I'm not getting the right answer when running the app:
private void SumNumbers()
{
int startNumber, endNumber;
Console.WriteLine("\nplease enter a start number: ");
startNumber = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("\nplease enter an end number: ");
endNumber = Convert.ToInt32(Console.ReadLine());
int result = 0;
for (int i=0; i<=startNumber; i=i+1)
{
result = result + i;
Console.WriteLine(i);
}
Console.ReadLine();
Console.WriteLine("The sum of Numbers between " + startNumber + " and " + endNumber + " is: " + result.ToString());
Console.ReadLine();
}
I'm getting this result: The sum of Numbers between 12 and 23 is: 78 when the result actually need to 210.
for (int i=0;i<=startNumber;i=i+1)
You are iterating from 0 to startNumber, when really you want to iterate like startNumber to endNumber.
Try
for (int i = startNumber; i <= endNumber; i = i+1)
Below is the working example.
I also added some logic to handle the checking of the input (whether it is correct or no) so the application doesn't break. This way the user experience is much better.
Here is the live working example: code
private void SumNumbers()
{
int startNumber, endNumber;
Console.WriteLine("\nplease enter a start number: ");
do
{
var input1 = Console.ReadLine();
if (Regex.IsMatch(input1, #"^\d+$"))
{
startNumber = Convert.ToInt32(input1); break;
}
else
{
Console.WriteLine("Input provided is invalid. Please enter a correct number: ");
}
} while (true);
Console.WriteLine("\nplease enter an end number: ");
do
{
var input2 = Console.ReadLine();
if (Regex.IsMatch(input2, #"^\d+$"))
{
endNumber = Convert.ToInt32(input2); break;
}
else
{
Console.WriteLine("Input provided is invalid. Please enter a correct number: ");
}
} while (true);
int min = Math.Min(startNumber, endNumber);
int max = Math.Max(startNumber, endNumber);
int result = 0;
for (int i = min; i <= max; i++)
{
result = result + i;
Console.WriteLine(i);
}
Console.WriteLine("The sum of Numbers between " + min + " and " + max + " is: " + result.ToString());
Console.ReadLine();
}
I'm new to programming and I've got confused on how to display the index number of a value in an array. I want to be able to type a random number and if the number I have entered is in the array, then it should tell me what the position (index) of the number is within the array.
For example if I enter the number 6, and 6 is in my array and it's index is 4, then the output should be "That number exists, it is positioned at 4 in the array". I've tried to do this but my code is the reverse of this, for example if I type in 6, then it looks for index 6 and outputs the number corresponding to index 6.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace searcharray
{
class Program
{
static void Main(string[] args)
{
int n = 10;
Random r = new Random();
int[] a;
a = new int[n + 1];
for (int i = 1; i <= n; i++)
a[i] = r.Next(1, 100);
for (int i = 1; i <= n; i++)
Console.WriteLine(" a [" + i + "] = " + a[i]);
Console.ReadLine();
Console.WriteLine("Enter a number: ");
int b = Convert.ToInt32(Console.ReadLine());
if (a.Contains(b))
{
Console.WriteLine("That number exists and the position of the number is: " + a[b]);
}
else
{
Console.WriteLine("The number doesn't exist in the array");
}
Console.WriteLine();
Console.ReadLine();
}
}
}
You can use Array.IndexOf(gives you the index of given value in Array) instead of a[b] like this:
if (a.Contains(b))
{
Console.WriteLine("That number exists and the position of the number is: " + Array.IndexOf(a, b));
}
else
{
Console.WriteLine("The number doesn't exist in the array");
}
You need to use Array.IndexOf() like below:
Console.WriteLine("That number exists and the position of the number is: " + Array.IndexOf(a, b));
Array.IndexOf returns -1 if the item dont exists in the array
var itemIndex = Array.IndexOf(a, b);
if (itemIndex != -1)
{
Console.WriteLine("That number exists and the position of the number is: " + itemIndex);
}
else
{
Console.WriteLine("The number doesn't exist in the array");
}
I'm working on Visual Studio about binary search in c#. My project about the computer find the user's guess number. So, I use tihs code in the main;
int min = 0; // minimum number in the array
int max = 100; // maximum number in the array
int middle = 50; // middle number in the array
int counter = 1;
string name, input;
int guess_number;
Console.WriteLine("Hello, this is a game that finding the number of in your mind. If you want to play so let me know you! ");
name = Console.ReadLine();
Console.WriteLine("Awesome welcome to the game " + name + " guess a number between " + min + " and " + max + " Please! ");
Console.WriteLine("Is your guess " + middle + " ?\nIf it's your guess then write (0) please!\nIf it's too high then write (1) please!\nIf it's too low then write (2) please!");
input = Console.ReadLine();
guess_number = Convert.ToInt32(input);
Console.WriteLine(" You select " + guess_number + " so, ");
do
{
counter += 1;
if (guess_number == 2)
{
min = middle + 1;
}
else if (guess_number == 1)
{
max = middle - 1;
}
else if (guess_number != 1 || guess_number != 2 || guess_number != 0)
{
Console.WriteLine(" Please write 0, 1 or 2 " + name);
}
middle = (min + max) / 2;
Console.WriteLine("Is your guess " + middle + " ?\nIf it's your guess then write (0) please!\nIf it's too high then write (1) please!\nIf it's too low then write (2) please!");
Console.WriteLine(counter + " times I tried for finding your number ");
} while (guess_number != 0);
Console.ReadKey();
However, output always repeat after the user write anything, why the reason about that, is there anyway to get the number?
from your description, I think you need to let user input new value to guess_number variable in the loop end otherwise the loop will not end from the condition guess_number != 0.
do
{
counter += 1;
if (guess_number == 2)
{
min = middle + 1;
}
else if (guess_number == 1)
{
max = middle - 1;
}
else if (guess_number != 1 || guess_number != 2 || guess_number != 0)
{
Console.WriteLine(" Please write 0, 1 or 2 " + name);
}
middle = (min + max) / 2;
Console.WriteLine("Is your guess " + middle + " ?\nIf it's your guess then write (0) please!\nIf it's too high then write (1) please!\nIf it's too low then write (2) please!");
Console.WriteLine(counter + " times I tried for finding your number ");
input = Console.ReadLine(); // let user key in new value.
guess_number = Convert.ToInt32(input);
} while (guess_number != 0);
the last readKey should be inside the while.
do
{
counter += 1;
if (guess_number == 2)
{
min = middle + 1;
}
else if (guess_number == 1)
{
max = middle - 1;
}
else if (guess_number != 1 || guess_number != 2 || guess_number != 0)
{
Console.WriteLine(" Please write 0, 1 or 2 " + name);
}
middle = (min + max) / 2;
Console.WriteLine("Is your guess " + middle + " ?\nIf it's your guess then write (0) please!\nIf it's too high then write (1) please!\nIf it's too low then write (2) please!");
input = Console.ReadLine();
guess_number = Convert.ToInt32(input);
Console.WriteLine(counter + " times I tried for finding your number ");
} while (guess_number != 0);
Write a program which will input a 4 digit number from the user and then print the first and 2nd last digit of the number
....
I can reverse the 4 numbers but can't do this..
Assuming the entered number is a four digit number.
class Program
{
static void Main(string[] args)
{
int num, sum = 0, r,pos = 0;
Console.WriteLine("Enter a Number : ");
num = int.Parse(Console.ReadLine());
while (num != 0)
{
pos++;
r = num % 10;
num = num / 10;
if(pos == 0)||(pos==2){
Console.WriteLine("Digit at"+ pos + "is : "+r);
}
}
Console.ReadLine();
}
}
string data, firstDigit, secondLastDigit = string.Empty;
Console.Write("Enter a 4 digit number : ");
data = Console.ReadLine();
while (data.Length != 4)
{
Console.WriteLine(data + " is not a 4 digit number");
Console.Write("Re-enter a 4 digit number : ");
data = Console.ReadLine();
}
firstDigit = data.Substring(0, 1);
secondLastDigit = data.Substring(data.Length - 2, 1);
Console.Write("First digit : " + firstDigit + " | " + "Second last digit : " + secondLastDigit);
Console.ReadLine();