Trying to make a guessing game in c# for a school projekt.

Updated 7 days ago in General
Share on Facebook0Tweet about this on Twitter0Share on Google+0Share on Reddit0
12 on January 11, 2017

Hello i’ve gotten this far but it closes when i’m trying my second guess and can’t figure out why.
Random random = new Random();
int randomNumber = random.Next(1, 100);
int guess = 0;
int guesses = 0;

Console.WriteLine(“Välkommen, gissa ett nummer mellan 1 och 100”);

{
guesses++;
Console.WriteLine(“Skriv in din gissning”);
int.TryParse(Console.ReadLine(), out guess);

if (randomNumber > guess)
{
Console.WriteLine(+guess + ” Är för högt!”);
}
else if (guess < randomNumber)
{
Console.WriteLine(+guess + “Är för lågt!”);
}
else
{
Console.WriteLine(“Du gissade ” + guess + “och det var rätt ” + randomNumber);
Console.WriteLine(“Det tog dig” + guesses + “innan du gissade rätt”);

}
Console.ReadKey();
}

  • Liked by
Reply
4 on January 11, 2017

Sorry for the swedish text by the way

7 days ago

Can you quickly translate it, it’s confusing, and please use code tags or pastebin, it’s easier and better.

7 days ago

And also put the whole script with the namespace and the main method.

7 days ago

You aren’t using any kind of a loop, so that’s why it’s not repeating, if you want this to run forever use while(true) loop.

7 days ago

Where should I add that? Really new to programming in c#

Show more replies
  • Liked by
Reply
Cancel
0 7 days ago

Now it’s translated and i’ve copied everything except “using system;

namespace ConsoleApplication1
{
 class Program
 {
 static void Main(string[] args)
 {
 Random random = new Random();
 int randomNumber = random.Next(1, 100);
 int guess = 0;
 int guesses = 0;
 Console.WriteLine("Welcome, guess a number between 1 and 100");
 {
 guesses++;
 Console.WriteLine("What's your guess");
 int.TryParse(Console.ReadLine(), out guess);
 if (randomNumber > guess)
 {
 Console.WriteLine(+guess + " Too high!");
 }
 else if (guess < randomNumber)
 {
 Console.WriteLine(+guess + " Too Low!");
 }
 else
 {
 Console.WriteLine("You guessed " + guess + "And it was correct! " + randomNumber);
 Console.WriteLine("It took you" + guesses + "before you got the correct number!");
 }
 Console.ReadKey();
 }
 }
 }
}

  • Liked by
Reply
Cancel
0 7 days ago

You aren’t using any kind of a loop, so when you guess the program ends. Put the code you want to be repeating in a loop. If you want the code to run forever do a while loop like this:

while(true)
{
    // The code you want to be repeated forever
}
  • Liked by
Reply
Cancel
4 7 days ago

I added a while(true) loop, so the program will ask you to gess infinitely until you get the number right in which case it displays a message and breaks from the loop using a break command:

// To break out of a while loop
break;

The whole code:

namespace ConsoleApplication1
{
 class Program
 {
 static void Main(string[] args)
 {
 Random random = new Random();
 int randomNumber = random.Next(1, 100);
 int guess = 0;
 int guesses = 0;
 while(true)
 {
 Console.WriteLine("Welcome, guess a number between 1 and 100");
 {
 guesses++;
 Console.WriteLine("What's your guess");
 int.TryParse(Console.ReadLine(), out guess);
 if (randomNumber > guess)
 {
 Console.WriteLine(+guess + " Too high!");
 }
 else if (guess < randomNumber)
 {
 Console.WriteLine(+guess + " Too Low!");
 }
 else
 {
 Console.WriteLine("You guessed " + guess + "And it was correct! " + randomNumber);
 Console.WriteLine("It took you" + guesses + "before you got the correct number!");
 break;
 }
 }
 Console.ReadKey();
 }
 }
 }
}

7 days ago

And also your code is messy, you should add comments to the code and use correct indentation (tabs).

7 days ago

https://msdn.microsoft.com/en-us/library/2aeyhxcd.aspx

7 days ago

Okay thanks, will work on that

7 days ago

This code works, you should probably lower the while loop before welcome to game print, and also when the user guesses a number there are some bugs, the next print is in the same line and it doesn’t wait for me to press enter.

Show more replies
  • Liked by
Reply
Cancel