Number Guessing Game. The program will generate a random number from 1 to 1000. The player will be given several chances to guess the number. The program gives a hint that the given number is lower or higher. If the player's guess is correct, give the player a single point. Tally the number of correct and wrong guesses that the player has made. After every round, display the tally (correct, wrong, and score) After a cycle the program will ask the user if he wants to continue playing. This is a console project. Below source code is my answer with the Question. Kindly check it sir/maam if i have the problem with the code. Thank you using System; using System.Collections.Generic; using System.Text; class Program { static void Main(string[] args) { while (true) { int randno = Newnum(1, 1001); int count = 1; while (true) { Console.Write("Enter a number between 1 and 1000(0 to quit):"); int input = Convert.ToInt32(Console.ReadLine()); if (input == 0) return; else if (input < randno) { Console.WriteLine("Low, try again."); ++count; continue; } else if (input > randno) { Console.WriteLine("High, try again."); ++count; continue; } else { Console.WriteLine("You guessed it! The number was {0}!", randno); Console.WriteLine("It took you {0} {1}.\n", count, count == 1 ? "try" : "tries"); break; } } } } static int Newnum(int min, int max) { Random random = new Random(); return random.Next(min, max); } }
Number Guessing Game.
- The
program will generate a random number from 1 to 1000. - The player will be given several chances to guess the number.
- The program gives a hint that the given number is lower or higher.
- If the player's guess is correct, give the player a single point.
- Tally the number of correct and wrong guesses that the player has made.
- After every round, display the tally (correct, wrong, and score)
- After a cycle the program will ask the user if he wants to continue playing.
This is a console project.
Below source code is my answer with the Question. Kindly check it sir/maam if i have the problem with the code. Thank you
using System;
using System.Collections.Generic;
using System.Text;
class Program
{
static void Main(string[] args)
{
while (true)
{
int randno = Newnum(1, 1001);
int count = 1;
while (true)
{
Console.Write("Enter a number between 1 and 1000(0 to quit):");
int input = Convert.ToInt32(Console.ReadLine());
if (input == 0)
return;
else if (input < randno)
{
Console.WriteLine("Low, try again.");
++count;
continue;
}
else if (input > randno)
{
Console.WriteLine("High, try again.");
++count;
continue;
}
else
{
Console.WriteLine("You guessed it! The number was {0}!",
randno);
Console.WriteLine("It took you {0} {1}.\n", count,
count == 1 ? "try" : "tries");
break;
}
}
}
}
static int Newnum(int min, int max)
{
Random random = new Random();
return random.Next(min, max);
}
}
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 1 images