Question number is the same as the random number?

ribbann

Member
Joined
Sep 4, 2016
Messages
5
Programming Experience
Beginner
Hi! im compleetly new at coding and started to watch barnacules codegasm series and one of the episodes is about making a simple magic8ball game, i took this idea and made it a little bit different, but now i need tome help :p

Here is my cocde atm (I know there is probably way better ways to write this :cheerful:) and yes it is a drinking game :victorious:
            // startingColor
            ConsoleColor OldColor = Console.ForegroundColor;

            // tells the user to pick a number
            GuessRight();            
            
           while(true)
            {
                //tells the user to "Pick a number"
               Console.ForegroundColor = ConsoleColor.White;
               Console.Write("Guess the number!! ");
               String Question = Console.ReadLine();

               Random RandomObj = new Random();
               Console.WriteLine("{0}", RandomObj.Next(10) + 1);

                if (RandomObj == Question)
                {
                    //How do i get the program to print out "That's Right.." 
                    //if the guessed number is the same as the random number? :) my guess was^^ x) 

                   Console.WriteLine("That's Right, You Dont Have To Drink");
                }
            }          


            Console.ForegroundColor = OldColor;

            Console.ReadKey();
        }

        static void GuessRight()
        {
            Console.ForegroundColor = ConsoleColor.DarkMagenta;
            Console.Write(".:Gissa");

            Console.ForegroundColor = ConsoleColor.Green;
            Console.Write(" R?tt");

            Console.ForegroundColor = ConsoleColor.Red;
            Console.Write(" Nummer:.");

            Console.ForegroundColor = ConsoleColor.DarkGray;
            Console.WriteLine(" V?lj ett nummer mellan 1-10, gissar du fel m?ste du dricka!");


so my question is, if you didnt already notice it, How do i get the program to print out "That's Right.." if the guessed number is the same as the random number? :)
 
Firstly, I have formatted your code and removed loads of the empty space. If you like lots of blank lines in your code that's fine but if you'd like us to read your code then please make it easy for us to read.

As for the question, if you want to know whether the random number you generated is equal to something then you actually have to compare the random number you generated. Look at these few lines of code:
Random RandomObj = new Random();
Console.WriteLine("{0}", RandomObj.Next(10) + 1);

if (RandomObj == Question)
'RandomObj' is a Random object used to generate random numbers. It's not a generated random number so what's the use of comparing it to anything? What in that code is the generated random number? It's 'RandomObj.Next(10) + 1' right? So, compare that. Just as is always the case, if you want to generate a value and then use that value multiple times, you assign the value to a variable and then use that variable multiple times. In short, generate your random number and assign it to a variable, then use that variable wherever you need that random number. If you need another random number, generate a another random number and assign it to the variable.

Also, a VERY important note on using the Random class: you should not be creating multiple Random objects and using each one to generate one number. You create one instance of the Random class and then use it multiple times. Get the creation of that Random object out of that loop and, in fact, out of that method.
 
Thnx fot the help. How do i compare the variable to what the user guessed?

How can you possibly be asking that question when your code already compares a variable to the user input? It's just comparing the wrong variable.
 
then i guess i dont understand you, this is what i got from what u wrote..

Random RandomObj = new Random();
Console.WriteLine("{0}", RandomObj.Next(10) + 1);


if (RandomObj.Next(10) + 1 == Question)
 
and what im actually asking is how do i get the program to print out "That's right", tho that might be the same question, i dont even know, im confused
 
We are here to help but it's not our job to teach you the fundamentals of programming. We should be able to say "assign a value to a variable" and you should be able to do that because that is about the most basic thing you do in programming. If you don't understand such simple concepts then you should follow the beginners tutorial link in my signature below and work your way through that and any other material that's appropriate to get a grounding in the basics. It's like someone asking how to make a table and, when they're told to hammer a nail into some wood, asking what's a hammer.
 

Latest posts

Back
Top Bottom