Home > other >  guessing game C# make a "youre close" answer
guessing game C# make a "youre close" answer

Time:06-23

I'm making a guessing game (guess between 1-100). and I need answer that I'm wrong but was close. I have the code (int diff = Math.Abs(guess - num); if (diff <= 3) but I don't know where to putt it.

I have made to that I have a number that is right so that it is easier to to see if I have done my code right (and will change it after)

 Console.WriteLine("gissa talet\nDu ska nu gissa ett tal mellan 1 ocn 100, så 
 varsågod..\nskriv in ett tal");
 int guess = Convert.ToInt32(str);
 //gonna make it random between 1-100
 int num = (50);
 {
 //when im right
 if (guess == num)
 {
    Console.WriteLine("rätt");
 }
 //when it´s to small guess
 else if (guess < num)
 Console.WriteLine("för lite");

 // to large guess
 if (guess > num)
    Console.WriteLine("för stort");

 // when i make it random nr and i guess over 100
 if (guess > 50)
    Console.WriteLine("gissa mellan 1-100");

 Console.ReadLine();
 }

CodePudding user response:

Well code looks little bit messy, so i also included suggestions how to clean it up. Also included suggestion where you can put check for close guess :)

Console.WriteLine("gissa talet\nDu ska nu gissa ett tal mellan 1 ocn 100, så varsågod..\nskriv in ett tal");

// User var, let the Visual Studio figure out the type, it looks cleaner :)
// Also you would need to use some sort of input which i don't see here, like
// var str = Console.ReadLine();

var guess = Convert.ToInt32(str);
//gonna make it random between 1-100
// No need for brackets around 50, just var num = 50;
// heads up, just use var num = new Random().Next(1, 100);

var num = 50;

//when im right
if (guess == num)
{
   Console.WriteLine("rätt");
}
// HERE WOULD BE BEST TO CHECK "CLOSE RANGE" GUESS :)
else if (Math.Abs(guess - num) <= 3)
{
    // sorry i don't know the language :(
    Console.WriteLine("You're very close !!");
}
//when it´s to small guess
else if (guess < num)
{
    Console.WriteLine("för lite");
}
// to large guess
// This was for some reason 'disconnected' from above if, but should be included in single check
// Also for that you could use switch statement :)
else
{
   Console.WriteLine("för stort");
}

// when i make it random nr and i guess over 100
// I think condition here was wrong, you compared against 50 for some reason, but we need 100 :)
// I think there you could also check if the guess > 0
if (guess > 100)
{
   Console.WriteLine("gissa mellan 1-100");
}

Console.ReadLine();

Another option is would to make "close range guess" check after the big if which cheks if guess is bigger/less/equal.

The difference would be that in that case both checks would be run, instaed of only one.

CodePudding user response:

If you have an if ... else if ... else if ... else construction, exactly one of the branches will execute. Right now, that is not really what you have: you have one if ... else if statement, followed by two separate ifs. So I would fix that first:

 Console.WriteLine("gissa talet");
 Console.WriteLine("Du ska nu gissa ett tal mellan 1 ocn 100, så varsågod..");
 Console.WriteLine("skriv in ett tal");

 int guess = Convert.ToInt32(str);

 //gonna make it random between 1-100
 int num = (50);

 //when im right
 if (guess == num)
 {
    Console.WriteLine("rätt");
 }
 //when it´s to small guess
 else if (guess < num)
 {
    Console.WriteLine("för lite");
 }    
 // to large guess
 else if (guess > num) 
 {
    Console.WriteLine("för stort");
 }
 // when i make it random nr and i guess over 100
 else if (guess > 50)
 {
    Console.WriteLine("gissa mellan 1-100");
 }

 Console.ReadLine();

Note that I also fixed the style so that the indentation matches and each branch of the if statement has {braces} around it.

So you now have if-statements that check

  • if the guess is exactly right
  • if the guess is (much) too small
  • if the guess is (much) too large
  • if the guess is over 50

Where in this logic would you like to check "not exactly right but close"?

I'll give you a hint: only exactly one of the cases will run, so if you do it too early the "exactly" right case will never run, and if you do it too late the "too small/large" case will never run.

Also, side note: your comment talks about a guess over 100 but then your code compares guess > 50. If you put comments in your code, then make sure that they match what the code does or your readers (including your future self) will get very confused which of the two is wrong!

CodePudding user response:

 int diff = Math.Abs(guess - num); 
 if (guess == num)
    Console.WriteLine("rätt");
 else if (diff <= 3)            // After 1 check, but before all the others? \
    Consoole.WriteLine("msg");  // depends on what exactly you want
 else if (guess < num)
    Console.WriteLine("för lite");
 else if (guess > num)
    Console.WriteLine("för stort");
 else if (guess > 100)
    Console.WriteLine("gissa mellan 1-100");

CodePudding user response:

You are looking for else if construction, something like this

//gonna make it random between 1-100
int num = (50); 

// for : let's restrict the number of attempts
for (int attempt = 1; attempt <= 10;   attempt) {
  Console.WriteLine("gissa talet\nDu ska nu gissa ett tal mellan 1 ocn 100, så varsågod..\nskriv in ett tal");
  //TODO: better use int.TryParse there 
  int guess = Convert.ToInt32(Console.ReadLine());

  if (guess == num) 
    Console.WriteLine("rätt");
  else if (Math.Abs(guess - num) <= 3)
    Console.WriteLine("you are close"); //TODO: Please, translate it
  else if (guess => 1 && guess < num)
    Console.WriteLine("too small");     //TODO: Please, translate it
  else if (guess <= 100 && guess > num)
    Console.WriteLine("too big");       //TODO: Please, translate it
  else 
    Console.WriteLine("gissa mellan 1-100");
}  

CodePudding user response:

I hope this will help you:

string str = Console.ReadLine();
int guess = Convert.ToInt32(str);
int num = (50);
int diff = guess - num;
if (guess == num)
{
    Console.WriteLine("rätt");
}
else if (guess > 100)
    Console.WriteLine("gissa mellan 1-100");

else if (diff <= 3)
    Console.WriteLine("");

else if (guess < num)
    Console.WriteLine("för lite");

else if (guess > num)
    Console.WriteLine("för stort");

Console.ReadLine();

CodePudding user response:

To add an alternative to the answers, C#10 now has pattern matching, which allows you to write it differently:

const int randomNumber = 50;

Console.WriteLine("Guess the number:");

string guess = Console.ReadLine();

Console.WriteLine(
    int.TryParse(guess, out var guessedNumber) switch
    {
        true => guessedNumber switch
        {
            (<1) or (>100) => "Please choose a number between 1 and 100",
            < randomNumber => "Too small",
            > randomNumber => "Too large",
            _ => "You guessed correctly"
        },
        false => "Please enter a valid number"
    });

However, randomNumber must be a constant for this to work.

  • Related