C
C#12mo 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♫12mo 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
IcyPhoenix12mo 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
Hackmagician12mo ago
return "You are basically on fire";
kekw
♫Woozy♫
♫Woozy♫12mo 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
Pobiega12mo ago
yes? abs is Math.Abs in C#
♫Woozy♫
♫Woozy♫12mo ago
i'm asuming else if (c#) is python's variant of elif can I use else if multiple times?
Pobiega
Pobiega12mo ago
yes however, consider using a switch instead switches in C# are very powerful, as they can do pattern matching
♫Woozy♫
♫Woozy♫12mo ago
I've never used that before. :/
Pobiega
Pobiega12mo ago
its similar in concept to if+elseif+elseif+...+else but its a nicer syntax Look at icePhoenix message above for an example
MODiX
MODiX12mo 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♫12mo 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
Pobiega12mo ago
Can you indicate where you are lost?
♫Woozy♫
♫Woozy♫12mo 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
Pobiega12mo ago
why do you need a tutorial? that example covers it all
♫Woozy♫
♫Woozy♫12mo ago
how do i write the so it's stands out from my normal text on discord btw? it was something like '''code'''
SinFluxx
SinFluxx12mo ago
$code
MODiX
MODiX12mo 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
Pobiega12mo 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♫12mo ago
switch (absDelta)
switch (absDelta)
it shows an error when i write (absDelta)
Pobiega
Pobiega12mo ago
sure, absDelta is just a placeholder variable here
♫Woozy♫
♫Woozy♫12mo ago
so it's normal?
Pobiega
Pobiega12mo 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♫12mo ago
i regret not going with if statements rn entire code is red and i have no clue what i'm doing.
Pobiega
Pobiega12mo ago
Show me.
♫Woozy♫
♫Woozy♫12mo ago
Pobiega
Pobiega12mo 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
linqisnice12mo 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♫12mo ago
thanks for the help!
Accord
Accord12mo 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
More Posts