method endgame() cannot be called - c#

So for the main class, I only have this code. Calling the cClass. THIS IS A DICE GAME
{
class Program
{
static void Main(string[] args)
{
cClass cs = new cClass();
}
}
For the Constructor class, which is cClass i have this code. The method endgame() is not working. This is where the problem located the when the dice game done battling, the endgame() is not working.
class cClass
{
public cClass()
{
Intialize();
}
public void Intialize()
{
sClass.round = 1;
while (sClass.com < 3 && sClass.player < 3)
{
start:
Console.WriteLine("Round (" + sClass.round + ")");
Console.WriteLine("Enter your guess: ( 1 - 6)");
sClass.guess = Convert.ToInt32(Console.ReadLine());
if (sClass.guess >= 7)
{
Console.WriteLine("Only enter number ranges from 1 - 6");
Console.ReadLine();
Console.Clear();
goto start;
}
sClass.playgame();
System.Threading.Thread.Sleep(1000);
Console.WriteLine("\nThe dice rolled is...");
Console.WriteLine(sClass.playerdie);
Console.WriteLine("\nPlayer guessed: " + sClass.guess);
Console.WriteLine("Computer guessed: " + sClass.comguess);
if ((sClass.comguess == sClass.playerdie) && (sClass.guess == sClass.playerdie))
{
Console.WriteLine("\nBoth guessed correctly! Restarting the round!\n");
Console.WriteLine("Player points: " + sClass.player);
Console.WriteLine("Computer points: " + sClass.com);
}
else if (sClass.guess == sClass.playerdie)
{
sClass.player++;
Console.WriteLine("\nPlayer guessed correctly you won!\n");
Console.WriteLine("Player points: " + sClass.player);
Console.WriteLine("Computer points: " + sClass.com);
sClass.round++;
}
else if (sClass.comguess == sClass.playerdie)
{
sClass.com++;
Console.WriteLine("\nComputer guessed correctly you lost...\n");
Console.WriteLine("Player points: " + sClass.player);
Console.WriteLine("Computer points: " + sClass.com);
sClass.round++;
}
else
{
Console.WriteLine("\nNo one got it correct restarting the round!\n");
Console.WriteLine("Player points: " + sClass.player);
Console.WriteLine("Computer points: " + sClass.com);
}
Console.ReadLine();
Console.Clear();
}
endgame();
}
public void endgame()
{
if (sClass.player > sClass.com)
{
Console.WriteLine("Player Win! Player vs Computer");
Console.WriteLine(" (" + sClass.player + ") vs (" + sClass.com + ")");
}
else if (sClass.player < sClass.com)
{
Console.WriteLine("Computer Win! Player vs Computer");
Console.WriteLine(" (" + sClass.player + ") vs (" + sClass.com + ")");
}
else
{
Console.WriteLine("Its a draw! Player vs Computer");
Console.WriteLine(" (" + sClass.player + ") vs (" + sClass.com + ")");
}
}
}
then I also have a static class , which is sClass.
static class sClass
{
public static int dice = 1;
public static int playerdie;
public static int guess;
public static int comguess;
public static int player;
public static int com;
public static int round;
public static Random r = new Random();
public static void playgame()
{
comguess = r.Next(1, 6);
playerdie = rolldie();
}
public static int rolldie()
{
int die = r.Next(1, 6);
return die;
}
}

So, you just said that there is no error. I think you just missed only 1 code. In cClass.cs Go to the Initialize() method, go at the endgame() then put the Console.ReadLine above the endgame
endgame();
Console.ReadLine();
Then it will show the endgame method.

Currently, your while loop says that it's checking on both simultaniously:
while (sClass.com < 3 && sClass.player < 3)
I said formerly if it should be an OR-statement, but that didn't work, because then either of them would still be true, continue-ing the while loop.
So it'll only stop the while loop if all statements are false.
Perhaps this code will work:
while (!(sClass.com >= 3 && sClass.player >= 3))
What I did now is reversing the while check, and now it checks if both are still false, and the ! will reverse that false to true. So if either of the scores becomes 3 now, it'll go out of the while loop.

Related

Choosing in menu wirh arrows on keyboard (C#)

I am trying to make a simple menu where I can choose with the arrows on the keyboard. I can run the code and the program doesn't give any errors, however I can not choose with the arrows and nothing happens if I press Enter, the choice always remains on the first choice (New customer). That will say, It always stays like this:
Hello and welcome! please choose type of registration:
*New customer <--
New staff
Service
Reparation
Garantie
My code this far is:
using System;
namespace uppdrag_2.cs
{
class Program
{
static void Main(string[] args)
{
string[] menuOptions = new string[] {"New customer\t", "New staff\t", "Serivce\t", "Reparation", "Garantie" };
int menuSelect = 0;
while (true)
{
Console.Clear();
Console.CursorVisible = false;
Console.WriteLine("Hello and welcome! Please choose type of registration:");
if (menuSelect == 0)
{
Console.WriteLine("* " + menuOptions[0] + "<--");
Console.WriteLine(menuOptions[1]);
Console.WriteLine(menuOptions[2]);
Console.WriteLine(menuOptions[3]);
Console.WriteLine(menuOptions[4]);
Console.ReadLine();
}
else if (menuSelect == 1)
{
Console.WriteLine(menuOptions[0]);
Console.WriteLine("* " + menuOptions[1] + "<--");
Console.WriteLine(menuOptions[2]);
Console.WriteLine(menuOptions[3]);
Console.WriteLine(menuOptions[4]);
Console.ReadLine();
}
else if (menuSelect == 2)
{
Console.WriteLine(menuOptions[0]);
Console.WriteLine(menuOptions[1]);
Console.WriteLine("* " + menuOptions[2] + "<--");
Console.WriteLine(menuOptions[3]);
Console.WriteLine(menuOptions[4]);
Console.ReadLine();
}
else if (menuSelect == 3)
{
Console.WriteLine(menuOptions[0]);
Console.WriteLine(menuOptions[1]);
Console.WriteLine(menuOptions[2]);
Console.WriteLine("* " + menuOptions[3] + "<--");
Console.WriteLine(menuOptions[4]);
Console.ReadLine();
}
else if (menuSelect == 4)
{
Console.WriteLine(menuOptions[0]);
Console.WriteLine(menuOptions[1]);
Console.WriteLine(menuOptions[2]);
Console.WriteLine(menuOptions[3]);
Console.WriteLine("* " + menuOptions[4] + "<--");
Console.ReadLine();
}
var keyPressed = Console.ReadKey();
if (keyPressed.Key == ConsoleKey.DownArrow && menuSelect != menuOptions.Length - 1)
{
menuSelect++;
}
else if (keyPressed.Key == ConsoleKey.UpArrow && menuSelect >= 1)
{
menuSelect--;
}
else if (keyPressed.Key == ConsoleKey.Enter)
{
switch (menuSelect)
{
case 0:
Newcustomer();
break;
case 1:
NewStaff();
break;
case 2:
Service();
break;
case 3:
Reparation();
break;
case 4:
Garantie();
break;
}
}
}
}
public static void Newcustomer(){
Console.WriteLine("You have chosen to registrate a new customer!");
Console.WriteLine("Please enter name of customer:");
string name = Console.ReadLine();
Console.WriteLine("Enter car brand:");
string carBrand = Console.ReadLine();
Console.WriteLine("Enter model of car");
string model = Console.ReadLine();
Console.WriteLine("Enter year model");
string yearModel = Console.ReadLine();
Console.WriteLine("Enter how many km the car has been driven");
string km = Console.ReadLine();
Console.WriteLine("Registration compleated!");
Console.WriteLine("You have registered a new customer! You have registrered" + " " + name + " " + "with the carbrand" + carBrand + " " + "of model" + " " + model + " " + "form year" + " " + yearModel + " " + "That has been driven" + " " + km + "km.");
Console.ReadKey();
}
public static void NewStaff(){
Console.WriteLine("You have chosen to registrer a new staffmember!");
Console.WriteLine("Please enter name of staffmember:");
string staffName = Console.ReadLine();
Console.WriteLine("Enter age:");
string ageStaff = Console.ReadLine();
Console.WriteLine("Enter work position of the new staffmember:");
string position = Console.ReadLine();
Console.WriteLine("Enter type of contract:");
string contract = Console.ReadLine();
Console.WriteLine("Enter date of the workers first day:");
string start = Console.ReadLine();
Console.WriteLine("Enter date of the workrs last day:");
string end = Console.ReadLine();
Console.WriteLine("Registration compleated!");
Console.WriteLine("You have registrered a new staffmember! The following information has been registered:\t" + "name: " + staffName + "\t" + "age: " + ageStaff + "\t" + "position: " + position + "\t" + "Contract: " + contract + "\t" + "First day: " + start + "\t" + "Last day: " + end);
Console.ReadKey();
}
public static void Service(){
Console.WriteLine("Please enter type of service:");
string service = Console.ReadLine();
Console.WriteLine("Enter the name of an registered customer. If customer is not already registered, please press Enter to return to menu and choose `New customer`");
string customerservice = Console.ReadLine();
Console.WriteLine("Enter staffmember responsible for this matter. If staffmember is new, please press Enter to return to the menu and choose ``New staff`");
string servicestaff = Console.ReadLine();
Console.WriteLine("Enter staring day of service:");
string serviceStart = Console.ReadLine();
Console.WriteLine("Enter deadline of servicematter:");
string deadline = Console.ReadLine();
Console.WriteLine("Service matter registrered!");
Console.WriteLine("Following information has been registrered:\t" + "Service matter: " + service + "\t" + "Customer: " + customerservice + "\t" + "Staff member responsible: " + servicestaff + "\t" + "Startdate of service matter: " + serviceStart + "\t" + "Deadline: " + deadline);
Console.ReadKey();
}
public static void Reparation(){
Console.WriteLine("You have choosen to registrer a reparation matter!");
Console.ReadLine();
Console.ReadKey();
}
public static void Garantie(){
Console.WriteLine("You have choosen to registrate a garantie matter!");
Console.WriteLine();
Console.ReadKey();
}
}
}
Anyone who could help me figure out what I`ve done wrong?
You can shorten your menu code quite a bit by using a for loop and a ternary if statement:
static void Main(string[] args)
{
string[] menuOptions = new string[] { "New customer\t", "New staff\t", "Serivce\t", "Reparation\t", "Garantie\t" };
int menuSelect = 0;
while (true)
{
Console.Clear();
Console.CursorVisible = false;
Console.WriteLine("Hello and welcome! Please choose type of registration:");
for (int i = 0; i < menuOptions.Length; i++)
{
Console.WriteLine((i == menuSelect ? "* " : "") + menuOptions[i] + (i == menuSelect ? "<--" : ""));
}
var keyPressed = Console.ReadKey();
if (keyPressed.Key == ConsoleKey.DownArrow && menuSelect != menuOptions.Length - 1)
{
menuSelect++;
}
else if (keyPressed.Key == ConsoleKey.UpArrow && menuSelect >= 1)
{
menuSelect--;
}
else if (keyPressed.Key == ConsoleKey.Enter)
{
switch (menuSelect)
{
case 0:
Newcustomer();
break;
case 1:
NewStaff();
break;
case 2:
Service();
break;
case 3:
Reparation();
break;
case 4:
Garantie();
break;
}
}
}
}
public static void Newcustomer()
{
Console.WriteLine("New Customer ...");
Console.Write("Press Enter to Continue");
Console.ReadLine();
}
public static void NewStaff()
{
Console.WriteLine("New Staff ...");
Console.Write("Press Enter to Continue");
Console.ReadLine();
}
public static void Service()
{
Console.WriteLine("Service ...");
Console.Write("Press Enter to Continue");
Console.ReadLine();
}
public static void Reparation()
{
Console.WriteLine("Reparation ...");
Console.Write("Press Enter to Continue");
Console.ReadLine();
}
public static void Garantie()
{
Console.WriteLine("Garantie ...");
Console.Write("Press Enter to Continue");
Console.ReadLine();
}
}
It works fine for me:

Program won't move onto next method

My program won't move onto the next method after it executes the main method.
Right now, it just prints out "Hey! Welcome to Tina's Dice Game. Let's Start!", and then stops. How do I fix this? I've messed around with it for a while, but nothing worked. Thank you for your help.
using System;
namespace Major_Coding_Assignment_1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hey! Welcome to Tina's Dice Game.");
Console.WriteLine("Let's start!");
}
public static int numberofInvokes = 0;
public void EvenOrOdd()
{
numberofInvokes += 1;
Random rnd = new Random();
int x = rnd.Next(1, 7);
int y = rnd.Next(1, 7);
int added = x + y;
if (added % 2 == 0)
{
Console.WriteLine("I got" + " " + x + " " + "and" + " " + y);
Console.WriteLine("Evens are better than odds.");
}
else
{
Console.WriteLine("I got" + " " + x + " " + "and" + " " + y);
Console.WriteLine("Odds are still cool!");
}
}
public void playAgain()
{
Console.WriteLine("Do you want to play again?");
string val = Console.ReadLine();
if (val == "yes")
{
EvenOrOdd();
}
else
{
Console.WriteLine("The number of times the dice was thrown is:" + " " + numberofInvokes);
Console.WriteLine("Thanks for playing, come play again soon!");
}
}
}
}
You need to call the methods you need from the main function like this...
static void Main(string[] args)
{
Console.WriteLine("Hey! Welcome to Tina's Dice Game.");
Console.WriteLine("Let's start!");
EvenOrOdd();
playAgain();
}
The better way is to call playAgain inside EvenOrOdd method to make things look cleaner.
static void Main(string[] args)
{
Console.WriteLine("Hey! Welcome to Tina's Dice Game.");
Console.WriteLine("Let's start!");
EvenOrOdd();
}
public static int numberofInvokes = 0;
public void EvenOrOdd()
{
numberofInvokes += 1;
Random rnd = new Random();
int x = rnd.Next(1, 7);
int y = rnd.Next(1, 7);
int added = x + y;
if (added % 2 == 0)
{
Console.WriteLine("I got" + " " + x + " " + "and" + " " + y);
Console.WriteLine("Evens are better than odds.");
}
else
{
Console.WriteLine("I got" + " " + x + " " + "and" + " " + y);
Console.WriteLine("Odds are still cool!");
}
playAgain(); //Call playAgain from here.
}
public void playAgain()
{
Console.WriteLine("Do you want to play again?");
string val = Console.ReadLine();
if (val == "yes")
{
EvenOrOdd();
}
else
{
Console.WriteLine("The number of times the dice was thrown is:" + " " + numberofInvokes);
Console.WriteLine("Thanks for playing, come play again soon!");
}
}
its important that you call your methods from main. I like to think of it like a recipe main is the first instruction but you need to tell the reader where to look next. The same is true for C#, you need to tell the compiler where to go next by calling the next function in the main one.
static void Main(string[] args)
{
//your start code
nextFunction();
}
public void nextFunction(){
//your next function code
}

I'm trying to return out of a method but I get a CS0219 warning

I'm trying to return out of a method in C#, but every time I run the program I get a message that I'm creating a variable but never using it, then I get errors that the current variable does not exist in current context.
I am relatively new to C#, and I might be really dumb but if anyone can help me that will be great!
The code:
using System;
class MainClass
{
public static void Main()
{
int health = 3;
int powerCrystals = 0;
int healthCrystals = 0;
int basicEnemyDamage = 1;
int basicEnemyScore = 10;
string basicEnemyDrops = "1 powerCrystal";
int score = 0;
// Basic Enemies Drop 1 powerCrystal.
// Moderate Enemies Drop 1 powerCrystal and 1 healthCrystal.
// Advanced Enemies Drop 2 powerCrystals and 1 healthCrystal.
// Bosses Drop 3 powerCrystals and 3 healthCrystals.
Console.WriteLine("Input your username:");
string userName = Console.ReadLine();
Console.WriteLine("Welcome, " + userName);
Console.WriteLine("To check your stats, write: stats");
TrollFight();
}
static void TrollFight()
{
Console.WriteLine(userName + "! There is a pack of trolls aproaching! Guess the right number to kill them!");
Console.WriteLine("Your current health is = " + health);
Console.WriteLine("Guess the correct number and then you can defeat the trolls! It is between 1-9!");
int guessedNumber = Convert.ToInt32(Console.ReadLine());
if (guessedNumber == 4)
{
Console.WriteLine("You killed the trolls! You gained: " + basicEnemyDrops);
score = score + basicEnemyDamage;
Console.WriteLine("Your score is = " + score);
return;
}
else
{
Console.WriteLine("WRONG! You took damage! Come on, guess again!");
health = health - basicEnemyDamage;
}
int guessedNumber2 = Convert.ToInt32(Console.ReadLine());
if (guessedNumber2 == 4)
{
Console.WriteLine("You killed the trolls! You gained: " + basicEnemyDrops);
score = score + basicEnemyScore;
Console.WriteLine("Your score is = " + score);
return;
}
else
{
Console.WriteLine("WRONG! You took damage! Come on, guess again! Your health is = 2");
health = health - basicEnemyScore;
}
int guessedNumber3 = Convert.ToInt32(Console.ReadLine());
if (guessedNumber3 == 4)
{
Console.WriteLine("You killed the trolls! You gained: " + basicEnemyDrops);
score = score + basicEnemyDamage;
Console.WriteLine("Your score is = " + score);
return;
}
else
{
Console.WriteLine("WRONG! You took damage! Come on, guess again! Your health is = 1");
Console.WriteLine("You are almost dead! You do not have any Health Crystals so you can't heal!");
health = health - basicEnemyScore;
}
int guessedNumber4 = Convert.ToInt32(Console.ReadLine());
if (guessedNumber4 == 4)
{
Console.WriteLine("You killed the trolls! You gained: " + basicEnemyDrops);
score = score + basicEnemyScore;
Console.WriteLine("Your score is = " + score);
return;
}
else
{
Console.WriteLine("WRONG! You died! Your final score = " + score);
health = health - basicEnemyDamage;
}
Console.WriteLine("To check your stats, write: stats");
}
}
I'm trying to return out of a method but it gives me a CS0219 warning
Ok a few things. The difference between a Void/Subroutine and a Function is that a Function returns something. Both your methods are voids, they don't return anything:
public static void Main()
static void TrollFight()
The problem with the variables is that they are out of scope.
Local - visible within the method
Private - visible to the class
Public - visible outside the assembly
One solution is to make the variables Private member variables:
class MainClass {
private int health = 3;
private int powerCrystals = 0;
private int healthCrystals = 0;
private int basicEnemyDamage = 1;
private int basicEnemyScore = 10;
private string basicEnemyDrops = "1 powerCrystal";
private int score = 0;
public static void Main() {
Run the project, this should fix it. The problem with using Private variable scope is it can get messy when multiple methods can change variables (without the other knowing). In this case we can use parameters to pass variables to voids or functions:
TrollFight(basicEnemyDamage);
...
static void TrollFight(string basicEnemyDamage) {
A good design will use all three with local variables:
class MainClass {
private int health = 3;
private string basicEnemyDrops = "1 powerCrystal";
private int score = 0;
public static void Main() {
...
}
static void TrollFight(string basicEnemyDamage) {
int powerCrystals = 0;
int healthCrystals = 0;
int basicEnemyDamage = 1;
int basicEnemyScore = 10;
I'd even change the void to return the Score:
static int TrollFight(string basicEnemyDamage, int score)
{
return score;
}
The Code:
using System;
public class MainClass {
private int health = 3;
private int score = 0;
private int powerCrystals = 0;
private int healthCrystals = 0;
private int basicEnemyDamage = 1;
private int basicEnemyScore = 10;
private string basicEnemyDrops = "1 powerCrystal";
public static void Main() {
//Basic Enemies Drop 1 powerCrystal.
//Moderate Enemies Drop 1 powerCrystal and 1 healthCrystal.
//Advanced Enemies Drop 2 powerCrystals and 1 healthCrystal.
//Bosses Drop 3 powerCrystals and 3 healthCrystals.
Console.WriteLine("Input your username:");
string userName = Console.ReadLine();
Console.WriteLine("Welcome, " + userName);
Console.WriteLine("To check your stats, write: stats");
TrollFight(userName);
}
static void TrollFight(string userName) {
Console.WriteLine(userName + "! There is a pack of trolls approaching! Guess the right number to kill them!");
Console.WriteLine("Your current health is = " + health.ToString());
Console.WriteLine("Guess the correct number and then you can defeat the trolls! It is between 1-9!");
int guessedNumber = Convert.ToInt32(Console.ReadLine());
if (guessedNumber == 4)
{
Console.WriteLine("You killed the trolls! You gained: " + basicEnemyDrops);
score = score + basicEnemyDamage;
Console.WriteLine("Your score is = " + score.ToString());
return;
}
else
{
Console.WriteLine("WRONG! You took damage! Come on, guess again!");
health = health - basicEnemyDamage;
}
int guessedNumber2 = Convert.ToInt32(Console.ReadLine());
if (guessedNumber2 == 4)
{
Console.WriteLine("You killed the trolls! You gained: " + basicEnemyDrops);
score = score + basicEnemyScore;
Console.WriteLine("Your score is = " + score.ToString());
return;
}
else
{
Console.WriteLine("WRONG! You took damage! Come on, guess again! Your health is = 2");
health = health - basicEnemyScore;
}
int guessedNumber3 = Convert.ToInt32(Console.ReadLine());
if (guessedNumber3 == 4)
{
Console.WriteLine("You killed the trolls! You gained: " + basicEnemyDrops);
score = score + basicEnemyDamage;
Console.WriteLine("Your score is = " + score.ToString());
return;
}
else
{
Console.WriteLine("WRONG! You took damage! Come on, guess again! Your health is = 1");
Console.WriteLine("You are almost dead! You do not have any Health Crystals so you can't heal!");
health = health - basicEnemyScore;
}
int guessedNumber4 = Convert.ToInt32(Console.ReadLine());
if (guessedNumber4 == 4)
{
Console.WriteLine("You killed the trolls! You gained: " + basicEnemyDrops);
score = score + basicEnemyScore;
Console.WriteLine("Your score is = " + score.ToString());
return;
}
else
{
Console.WriteLine("WRONG! You died! Your final score = " + score.ToString());
health = health - basicEnemyDamage;
}
Console.WriteLine("To check your stats, write: stats");
}
}

How to get out of a while-loop in c#

Here I am trying to have the user enter "YES" to continue with the game but i'm stuck in the while-loop. This is also my first post so I don't the correct way to post ie. copy and paste? Also is there a indentation shortcut for Visual Studio? Thanks for helping.
using System;
namespace BIT_142_Major_Assignment_1
{
class Program
{
static void Main(string[] args)
{
int numGames = 0;
String answer = "";
Random rand = new Random();
int Dice1 = rand.Next(1, 7);
int Dice2 = rand.Next(1, 7);
Console.WriteLine("Hey! Welcome to Andy’s Dice Game.");
Console.WriteLine("Let’s start!");
{
while (true)
{
numGames++;
Console.WriteLine("Dice 1: " + Dice1);
Console.WriteLine("Dice 2: " + Dice2);
Console.WriteLine("I got " + Dice1 + " and " + Dice2 + "!");
if ((Dice1 + Dice2) % 2 == 0)
{
Console.WriteLine("Evens are better than odds!");
}
else
{
Console.WriteLine("Odds are still cool!");
Console.WriteLine("Do you want to play again?");
answer = Console.ReadLine();
if (answer == "YES")
{
continue;
}
else
{
Console.WriteLine("The number of times the dice was thrown is: " + numGames);
Console.WriteLine("Nice Game!");
Console.WriteLine("Thank for playing, Come play again soon!");
break;
}
}
}
}
}
}
}

How to start over with my code? C# Console [duplicate]

This question already has answers here:
How to loop a Console App
(6 answers)
Closed 6 years ago.
I wanted to try how the if conditional works so I created this code almost by myself. I also had problems with random into int.
Here's my code:
using System;
namespace Bigger_Smaller_Equal
{
class Program
{
static void Main(string[] args)
{
int min = 1;
int max = 100;
Random rnd = new Random();
int gen = rnd.Next(min, max);
Console.WriteLine("My Number is : " + gen + "!");
Console.WriteLine("Tell me your number:");
string typ = Console.ReadLine();
int num = int.Parse(typ);
if (num == gen)
{
Console.WriteLine(num + " is Equal to " + gen);
}
else if (num > gen)
{
Console.WriteLine(num + " Is Bigger than " + gen);
}
else if (num < gen)
{
Console.WriteLine(num + " Is Smaller than " + gen);
}
Console.WriteLine("Press Any Key to exit.");
Console.ReadLine();
}
}
}
How to make the console stop, so it will allow me to enter another number?
Basically:
I write a number it tells me if its smaller bigger or equal to number which was randomly generated
After I press enter instead of closing the console the number will be generated again and I can write new number and so on.
Here's an example using goto, although it is not recommended for more complex applications as you could end up creating endless loops. Feel free to try it out
static void Main(string[] args)
{
int min = 1;
int max = 100;
Random rnd = new Random();
again:
int gen = rnd.Next(min, max);
Console.WriteLine("My Number is : " + gen + "!");
Console.WriteLine("Tell me your number:");
string typ = Console.ReadLine();
int num = int.Parse(typ);
if (num == gen)
{
Console.WriteLine(num + " is Equal to " + gen);
}
else if (num > gen)
{
Console.WriteLine(num + " Is Bigger than " + gen);
}
else if (num < gen)
{
Console.WriteLine(num + " Is Smaller than " + gen);
}
repeat:
Console.WriteLine("Play again? (Y/N)");
string ans = Console.ReadLine();
switch (ans.ToUpper())
{
case "Y": goto again; break;
case "N": break; //continue
default: goto repeat; break;
}
}
You can use console.ReadKey() insted of using console.ReadLine().
console.ReadLine() wait for input set of character thats why you console window is apperre there after pressing any key.
You can use "do while" or "while" operator.If you dont want to use while(true) , you can use this diffirent way. I mean that when user enter 0 or -1 this system can stop. while()
bool repeat = true;
do
{
Console.WriteLine("Enter value ");
string typ = Console.ReadLine();
int num = int.Parse(typ);
if (num!=0)
// bla bla bla.
else
repeat = false;
}while (repeat);

Categories