C
C#17mo ago
♫Woozy♫

❔ Number guessing game.

I'm trying to make a program that generates a random number between 1-100, let's the user guess till they get the correct number, tells the use if they guessed too high/low and also let's them know if they guess correctly and if so, tell's them how many guesses it took. But I want to make it more complicated by letting the user know when they are close to the number. This is how my code looks so far: using System.Diagnostics.CodeAnalysis; Random random = new Random(); bool playAgain = true; int min = 1; int max = 101; int guess; int number; int guesses; while (playAgain) { guess = 0; guesses = 0; number = random.Next(min, max); while (guess != number) { Console.WriteLine("Try to guess the randomly generated number " + min + "-" + max + ":"); guess = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Guess: " + guess); if (guess < number) { Console.WriteLine("Number " + guess + " is too low..."); } else if (guess > number) { Console.WriteLine("Number " + guess + " is too high!"); } guesses++; } Console.WriteLine("You won! Good job!"); Console.WriteLine("Total guesses; " + guesses); }
29 Replies
♫Woozy♫
♫Woozy♫OP17mo ago
If the correct number is 45 and the user guesses 44, i want the program to say "It's getting really warm" but not tell the user if the number is higher or lower.
IcyPhoenix
IcyPhoenix17mo ago
just get a abs value of the delta and have a switch case that deals with close values probably easiest way i can think of and use the default just the standard response
switch (absDelta)
{
case 1:
return "You are basically on fire";
case > 1 and <= 5:
return "Some other message to do with warmth";
case > 5 and <= 10:
return "....";
default:
return "standard message" ;
}
switch (absDelta)
{
case 1:
return "You are basically on fire";
case > 1 and <= 5:
return "Some other message to do with warmth";
case > 5 and <= 10:
return "....";
default:
return "standard message" ;
}
Hackmagician
Hackmagician17mo ago
return "You are basically on fire";
kekw
♫Woozy♫
♫Woozy♫OP17mo ago
in python I did it like this: print("Hi, welcome to the number guessing game.") import random number = random.randint(1,100) guess = 0 while guess !=number: guess = int(input("Guess:")) if abs(guess - number) <=3 and guess !=number: print("You're really close, keep going.") elif guess == number: print("You guessed correct!") elif guess < number: print("Higher!") else: print("Lower!") Can I do it in a similar way on C#?
Pobiega
Pobiega17mo ago
yes? abs is Math.Abs in C#
♫Woozy♫
♫Woozy♫OP17mo ago
i'm asuming else if (c#) is python's variant of elif can I use else if multiple times?
Pobiega
Pobiega17mo ago
yes however, consider using a switch instead switches in C# are very powerful, as they can do pattern matching
♫Woozy♫
♫Woozy♫OP17mo ago
I've never used that before. :/
Pobiega
Pobiega17mo ago
its similar in concept to if+elseif+elseif+...+else but its a nicer syntax Look at icePhoenix message above for an example
MODiX
MODiX17mo ago
IcyPhoenix
switch (absDelta)
{
case 1:
return "You are basically on fire";
case > 1 and <= 5:
return "Some other message to do with warmth";
case > 5 and <= 10:
return "....";
default:
return "standard message" ;
}
switch (absDelta)
{
case 1:
return "You are basically on fire";
case > 1 and <= 5:
return "Some other message to do with warmth";
case > 5 and <= 10:
return "....";
default:
return "standard message" ;
}
Quoted by
<@105026391237480448> from #Number guessing game. (click here)
React with ❌ to remove this embed.
♫Woozy♫
♫Woozy♫OP17mo ago
How would that look in my program? I've been studying c# for just a few hours total so not any good at it yet I'll try playing around with switches a bit I'm completely lost 💀
Pobiega
Pobiega17mo ago
Can you indicate where you are lost?
♫Woozy♫
♫Woozy♫OP17mo ago
This entire part so far. I want to find a tutorial on it but so far none of the tutorials I've found use numbers
Pobiega
Pobiega17mo ago
why do you need a tutorial? that example covers it all
♫Woozy♫
♫Woozy♫OP17mo ago
how do i write the so it's stands out from my normal text on discord btw? it was something like '''code'''
SinFluxx
SinFluxx17mo ago
$code
MODiX
MODiX17mo ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat If your code is too long, post it to: https://paste.mod.gg/
Pobiega
Pobiega17mo ago
tripple backtick, the letters cs, then end with tripple backtick for a whole block for inline, use a single backtick for start and end inline code
public class BlockOfCode ...
public class BlockOfCode ...
♫Woozy♫
♫Woozy♫OP17mo ago
switch (absDelta)
switch (absDelta)
it shows an error when i write (absDelta)
Pobiega
Pobiega17mo ago
sure, absDelta is just a placeholder variable here
♫Woozy♫
♫Woozy♫OP17mo ago
so it's normal?
Pobiega
Pobiega17mo ago
assuming you didn't make a variable with that name... yes? its just a variable name in this example, probably containing the absolute delta value between the answer and your guess
♫Woozy♫
♫Woozy♫OP17mo ago
i regret not going with if statements rn entire code is red and i have no clue what i'm doing.
Pobiega
Pobiega17mo ago
Show me.
♫Woozy♫
♫Woozy♫OP17mo ago
Pobiega
Pobiega17mo ago
case1: should be case 1: you have red everywhere because the compiler can't understand what you are trying to do Also part of the reason why I suggested you don't use top-level statements
linqisnice
linqisnice17mo ago
int number = 1;
var switchStatement = number switch
{
1 => "getting very hot",
< 5 => "colder",
< 10 => "cold",
_ => "you aint even close"
};

// This will write "getting very hot" since number = 1.
// if you change it to between 1 and 5, it will write "colder"
Console.WriteLine(switchStatement);
int number = 1;
var switchStatement = number switch
{
1 => "getting very hot",
< 5 => "colder",
< 10 => "cold",
_ => "you aint even close"
};

// This will write "getting very hot" since number = 1.
// if you change it to between 1 and 5, it will write "colder"
Console.WriteLine(switchStatement);
u can do this as well the number (or what they refer to as abs) would be the difference between the user input and the correct value (so shouldnt be hardcoded top level statements are really confusing yeah lol
♫Woozy♫
♫Woozy♫OP17mo ago
thanks for the help!
Accord
Accord17mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server