mercredi 9 octobre 2019

Random number guessing game not working C#

I am trying to get this random number guessing game working. The program runs but it doesn't give the you won message when you enter the correct number and the hint feature does not give the feed back it is supposed too. Any help appreciated.

using System;

namespace randomNumberGame
{
    class Program
    {
        static void Main(string[] args)
        {
            Random r = new Random();
            var val = r.Next(1, 100);
            var guess = 0;
            bool correct = false;
            var attemptsLeft = 5;


            Console.WriteLine("I'm thinking of a number between 1 and 100.");

            while (!correct && attemptsLeft >=  1)
            {

                Console.Write("You have " + attemptsLeft + " lives left. Please enter your Guess: ");
                string input = Console.ReadLine();
                var message = "";
                var difference = val - guess;

                if (!int.TryParse(input, out guess))
                {
                    Console.WriteLine("That's not a number.");
                    continue;
                }
                if (difference == 0)
                {
                    Console.WriteLine("You have won");
                    correct = true;
                }


                else
                {

                    if (Math.Abs(difference) >= 50)
                    {
                        message = Math.Sign(difference) == -1 ? "Very High" : "Very Low";
                    }
                    else if (Math.Abs(difference) < 50 && Math.Abs(difference) >= 20)
                    {
                        message = Math.Sign(difference) == -1 ? "High" : "Low";
                    }
                    else if (Math.Abs(difference) < 20 && Math.Abs(difference) >= 10)
                    {
                        message = Math.Sign(difference) == -1 ? "Moderatley High" : "Moderately Low";
                    }
                    else if (Math.Abs(difference) < 10)
                    {
                        message = Math.Sign(difference) == -1 ? "Somewhat High" : "Somewhat Low";
                    }
                    else Console.WriteLine("???");
                }
                attemptsLeft--;


            }


        }
    }
}



Aucun commentaire:

Enregistrer un commentaire