Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello, I'm making a number guessing game in C# for homework. When I run it, if the guess is lower or higher than the random number, it just loops "Guess higher" or guess lower". Any help would be appreciated.




  using System;

namespace Lab7
{
    class Program
    {
        static void Main(string[] args)
        {
            //Problem1();
            //Problem2();
            Problem3();
            //Problem4();
            //Problem5();
            //Problem6();
            //Problem7();
            //Problem8();
            //Problem9();
            //Problem10();

            Console.ReadKey();
 static void Problem3()
        {
            Random random = new Random();
            int numNums = 2;
            int GuessCount = 1;
            int current = 0;
            Console.WriteLine("Enter amount of numbers to guess from (100 for 1 - 100) ");
            numNums = int.Parse(Console.ReadLine());
            int randy = random.Next(1, numNums);
            Console.WriteLine("What is your guess? ");
            current = int.Parse(Console.ReadLine());
            if (current == randy)
            { Console.WriteLine("Good job!  Guessed in {0} guesses!", GuessCount); }

            while (current != randy)
            {
                
                if (current > randy)
                {
                    Console.WriteLine("Guess lower.");
                    GuessCount++;
                    continue;
                }
                if (current < randy)
                {
                    Console.WriteLine("Guess higher.");
                    GuessCount++;
                    continue;
                }
            }


What I have tried:

We recently finished with Python, now we're trying problems we solved in Python in C#. I have no clue what to do here lol
Posted
Updated 28-Nov-21 13:04pm
v3

1 solution

Figured it out!

static void Problem3()
        {
            Random random = new Random();
            int numNums = 2;
            int GuessCount = 1;
            int current = 0;
            Console.WriteLine("Enter amount of numbers to guess from (100 for 1 - 100) ");
            numNums = int.Parse(Console.ReadLine());
            int randy = random.Next(1, numNums);
           
            

            while (current != randy)
            {
                Console.WriteLine("What is your guess? ");
                current = int.Parse(Console.ReadLine());
                if (current == randy)
                { Console.WriteLine("Good job!  Guessed in {0} guesses!", GuessCount); }
                if (current > randy)
                {
                    Console.WriteLine("Guess lower.");
                    GuessCount++;
                    continue;
                }
                if (current < randy)
                {
                    Console.WriteLine("Guess higher.");
                    GuessCount++;
                    continue;
                }
            }
 
Share this answer
 
Comments
PIEBALDconsult 28-Nov-21 23:51pm    
Please don't try to answer your own question.
Grelm 29-Nov-21 1:53am    
Why not? I was going to just delete it, but it let me enter the solution myself. And I figure if someone else is having similar issues then they might be helped by my solution.
PIEBALDconsult 29-Nov-21 7:59am    
Certainly, but it's better to update the question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900